Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Sequelize关系查询返回重复数据_Javascript_Node.js_Sequelize.js - Fatal编程技术网

Javascript Sequelize关系查询返回重复数据

Javascript Sequelize关系查询返回重复数据,javascript,node.js,sequelize.js,Javascript,Node.js,Sequelize.js,我正在使用Sequelize关系查询指定客户的客户订单 index.js service.js 结果 期望 您只需从查询中删除raw:true 因为它将返回普通/平面对象,这将转换对象的外观 exports.getOrders = function (id) { return customerModel.findAll({ // raw: true, // <------ Just remove this line include: [{

我正在使用Sequelize关系查询指定客户的客户订单

index.js

service.js

结果

期望

您只需从查询中删除raw:true

因为它将返回普通/平面对象,这将转换对象的外观

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

    }).then(r => r);
};
注意:你应该根据你的要求把where条件放在上层 逻辑

尝试从查询中删除原始键值

Finder方法用于从数据库查询数据。是的 不返回普通对象,而是返回模型实例。因为 finder方法返回模型实例您可以调用任何模型实例 实例文档中描述的结果的成员

如果您想获得没有元/模型信息的数据,那么使用

{ plain: true }
很好的续集例子

例如:


我正在为customer和orders[[Order],[Order]]@rod获取元数据,这是在console内部,如果您希望看到完整的响应生成JSON.stringifyyour_obj和console,您将了解这一点。啊,好吧……如果这是针对API端点的,我会将其编码为}。thenr=>JSON.stringifyr@rod,不,它将返回与您想要的相同的数据,但它不会显示在控制台中,要在控制台中看到它,您只需要执行JSON.stringify,明白我的意思了吗?好的,我想我现在可以了。我通过网络浏览器测试了一下,明白你的意思了。非常感谢。
[ { 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 } ]
[ { 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 },   
    }]
]
exports.getOrders = function (id) {
    return customerModel.findAll({
        // raw: true, // <------ Just remove this line
        include: [{
            model: orderModel,
            where: { customer_idcustomer: id }
        }],

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