Sequelize relationship query returns duplicate data


Sequelize relationship query returns duplicate data



I'm querying customer orders for a specified customer using Sequelize relationships.



index.js


var results2 = await customerService.getOrders(1);
console.log(results2);



service.js


exports.getOrders = function (id) {
return customerModel.findAll({
raw: true,
include: [{
model: orderModel,
where: { customer_idcustomer: id }
}],

}).then(r => r);
};



results


[ { idcustomer: 1,
customername: 'hello world',
'orders.idorder': 1,
'orders.orderdesc': 'order description 1',
'orders.customer_idcustomer': 1 },
{ idcustomer: 1,
customername: 'hello world',
'orders.idorder': 2,
'orders.orderdesc': 'Test 456',
'orders.customer_idcustomer': 1 },
{ idcustomer: 1,
customername: 'hello world',
'orders.idorder': 3,
'orders.orderdesc': 'Test 123',
'orders.customer_idcustomer': 1 } ]



expected


[ { idcustomer: 1,
customername: 'hello world',
'orders: [{
'orders.idorder': 1,
'orders.orderdesc': 'order description 1',
'orders.customer_idcustomer': 1 },
},
{
'orders.idorder': 2,
'orders.orderdesc': 'order description 2',
'orders.customer_idcustomer': 1 },
},
{
'orders.idorder': 3,
'orders.orderdesc': 'order description 3',
'orders.customer_idcustomer': 1 },
}]
]




2 Answers
2



All you need is to remove raw: true, from query ,


raw: true,



as it will return plain/flat object , and that will convert your object as it looks now.


exports.getOrders = function (id) {
return customerModel.findAll({
// raw: true, // <------ Just remove this line
include: [{
model: orderModel,
where: { customer_idcustomer: id }
}],

}).then(r => r);
};



Note : You should put the where condition in upper level as per your
logic


exports.getOrders = function (id) {
return customerModel.findAll({
where: { id: id } ,
// raw: true, // <------ Just remove this line
include: [{
model: orderModel
}]
}).then(r => r);
};





But then it has the return has all the metadata.
– Rod
Jul 2 at 13:25





@Rod , Check the updated answer
– Vivek Doshi
Jul 2 at 14:01





I'm getting metadata for customer and the following for orders [[Order],[Order]]
– Rod
Jul 4 at 5:40



metadata


customer


[[Order],[Order]]





@rod , that is inside console , if you want to see full response make JSON.stringify(your_obj) and console that out you will get the idea.
– Vivek Doshi
Jul 4 at 5:43


JSON.stringify(your_obj)


console





@rod , no It will return same data as you wanted but it wan't be shown in console, to see that in console only you need to do JSON.stringify , getting my point ?
– Vivek Doshi
Jul 4 at 6:04


JSON.stringify



Try removing raw key value from your query.


raw



Finder methods are intended to query data from the database. They do
not return plain objects but instead return model instances. Because
finder methods return model instances you can call any model instance
member on the result as described in the documentation for instances.



If you want to get the data without meta/model information then map your results using


{ plain: true }



Good sequelize examples in docs



Example:


const getPlainData = records => records.map(record =>
record.get({ plain: true }));

// Your code
return customerModel.findAll({
// raw: true, <= remove
include: [{
model: orderModel,
where: { customer_idcustomer: id }
}],

}).then(getPlainData);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy