Sequelize 关系查询返回重复数据

Sequelize relationship query returns duplicate data(Sequelize 关系查询返回重复数据)
本文介绍了Sequelize 关系查询返回重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

index.js

var results2 = await customerService.getOrders(1);控制台.log(结果2);

service.js

exports.getOrders = function (id) {返回 customerModel.findAll({生:真的,包括: [{型号:订单型号,其中:{ customer_idcustomer:id }}],}).then(r => r);};

结果

[ { idcustomer: 1,客户名称:'你好世界','orders.idorder': 1,'orders.orderdesc': '订单描述 1','orders.customer_idcustomer': 1 },{ id客户:1,客户名称:'你好世界','orders.idorder': 2,'orders.orderdesc': '测试 456','orders.customer_idcustomer': 1 },{ id客户:1,客户名称:'你好世界','orders.idorder': 3,'orders.orderdesc': '测试 123','orders.customer_idcustomer': 1 } ]

预期

[ { idcustomer: 1,客户名称:'你好世界','命令: [{'orders.idorder': 1,'orders.orderdesc': '订单描述 1','orders.customer_idcustomer': 1 },},{'orders.idorder': 2,'orders.orderdesc': '订单描述 2','orders.customer_idcustomer': 1 },},{'orders.idorder': 3,'orders.orderdesc': '订单描述 3','orders.customer_idcustomer': 1 },}]]

解决方案

你只需要从查询中删除 raw: true,

因为它将返回普通/平面对象,这会将您的对象转换为现在的样子.

exports.getOrders = function (id) {返回 customerModel.findAll({//raw: true,//<------ 删除这一行包括: [{型号:订单型号,其中:{ customer_idcustomer:id }}],}).then(r => r);};

<小时><块引用>

注意:您应该根据您的要求将 where 条件放在上层逻辑

exports.getOrders = function (id) {返回 customerModel.findAll({其中: { id: id } ,//raw: true,//<------ 删除这一行包括: [{型号:订单型号}]}).then(r => r);};

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

解决方案

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

这篇关于Sequelize 关系查询返回重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)