sequelize.query() returns same result twice

一个人想着一个人 提交于 2020-01-14 07:13:09

问题


I am working in nodejs project in that using sequelize for connecting mysql database. I am also using sequelize-values for getting raw data from Sequelize instances.

I have written below code

var Sequelize = require('sequelize');
require('sequelize-values')(Sequelize);
var sequelizeObj = new Sequelize('mysql://root:@localhost/database');

sequelizeObj.authenticate().then(function (errors) {
    console.log(errors)
});

sequelizeObj.query("SELECT * FROM `reports` WHERE `id` = 1200").then(function (result) {

    });

Now the table reports have only 1 record for id 1200, But the result gives two objects for same records, Means both records are same of id 1200.

[ [ { id: 1200,
  productivity_id: 9969,
  gross_percentage_points: 100 } ],
[ { id: 1200,
  productivity_id: 9969,
  gross_percentage_points: 100 } ] ]

Let me know what I am doing wrong?


回答1:


The first object is the result object, the second is the metadata object (containing affected rows etc) - but in mysql, those two are equal.

Passing { type: Sequelize.QueryTypes.SELECT } as the second argument will give you a single result object (metadata object omitted

https://github.com/sequelize/sequelize/wiki/Upgrading-to-2.0#changes-to-raw-query




回答2:


Try :

sequelizeObj.query("SELECT * FROM `reports` WHERE `id` = 1200", { type: Sequelize.QueryTypes.SELECT }).then(function (result) {
    });



回答3:


Add { type: Sequelize.QueryTypes.SELECT } as the second argument, also remember to import Sequelize first.



来源:https://stackoverflow.com/questions/33232147/sequelize-query-returns-same-result-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!