我在节点中使用sequlize,我必须在sequlize查询中实现list对象中的add属性。
.show li {
display: inline-block;
}
我希望添加对象' myCustomers'属性,但列表中的所有属性都不包含此属性。
这是为什么?怎么解决它?
答案 0 :(得分:1)
partner.findAll
是异步的。当你的代码通过循环并初始化promises时,请求才刚刚被发送出去; .then
仅在初始化它们的线程完成后解析,因此partnerList
不会在return partnerList
之前填充Promise.all
。相反,在所有findAll
s上使用member.findAll({
where:{
isPartner:1
}
}).then(async (partnerList) => {
await Promise.all(partnerList.map(({ dataValues: { no } }) => (
partner.findAll({
include: [{
model: member,
required: true,
}],
where:{
partner_no : no
}
}).then(myCustomers => {
partnerList[i].dataValues.myCustomers = myCustomers.length;
}))));
return partnerList;
}).then(partnerList => {
res.render('partner/partners', {partnerList: partnerList})
})
:
async
但是对于await
/ .then
,如果没有const partnerList = await member.findAll({
where:{
isPartner:1
}
});
await Promise.all(partnerList.map(async ({ dataValues: { no } }) => {
const myCustomers = await partner.findAll({
include: [{
model: member,
required: true,
}],
where:{
partner_no : partner_no
}
});
partner.dataValues.myCustomers = myCustomers.length;
}));
res.render('partner/partners', {partnerList: partnerList});
s,它会更容易阅读:
time range
2018-03-04 00:00:06.520 0
2018-03-04 00:00:07.130 0
2018-03-04 00:00:07.850 1
2018-03-04 00:00:08.420 1
2018-03-04 00:00:09.210 2
2018-03-04 00:00:10.070 2
2018-03-04 00:00:10.840 3
2018-03-04 00:00:11.230 3
2018-03-04 00:00:11.980 4
2018-03-04 00:00:12.560 4
2018-03-04 00:00:13.120 0
2018-03-04 00:00:13.790 0
2018-03-04 00:00:14.330 1
2018-03-04 00:00:15.280 1
2018-03-04 00:00:15.960 2
2018-03-04 00:00:16.420 2
2018-03-04 00:00:17.090 3