Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize.query() returns same result twice

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?

like image 775
mujaffars Avatar asked Oct 20 '15 08:10

mujaffars


3 Answers

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

like image 164
Jan Aagaard Meier Avatar answered Oct 11 '22 22:10

Jan Aagaard Meier


Try :

sequelizeObj.query("SELECT * FROM `reports` WHERE `id` = 1200", { type: Sequelize.QueryTypes.SELECT }).then(function (result) {
    });
like image 29
Nado Avatar answered Oct 12 '22 00:10

Nado


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

like image 39
Onengiye Richard Avatar answered Oct 12 '22 00:10

Onengiye Richard