admin管理员组

文章数量:1401208

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 },   
    }]
]

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 },   
    }]
]
Share Improve this question edited Jul 1, 2018 at 4:01 Rod asked Jun 30, 2018 at 22:16 RodRod 15.5k35 gold badges134 silver badges264 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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

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);
};

Try removing raw key value from your query.

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);

In my case, having

raw: true

in the options didn't make any difference.

I added

distinct: true 

and the issue disappeared.

I was using findAndCountAll, though.

Documentation: https://sequelize/master/class/lib/model.js~Model.html

本文标签: javascriptSequelize relationship query returns duplicate dataStack Overflow