在循环中续集查询执行



下面是 iam 调用 addUpdateDailyLeads,数组如下

[{
"yyyymmdd": "20191124",
"admin_login":"rasheed.s",
"category":"PO",
"amount":10,
"office_id":10000,
"new_leads_attempted":10
},
{
"yyyymmdd": "20191124",
"admin_login":"rasheed.s",
"category":"PO",
"amount":10,
"office_id":10000,
"new_leads_attempted":10
},
{
"yyyymmdd": "20191125",
"admin_login":"prajeed.av",
"category":"FHL",
"amount":10,
"office_id":10000,
"new_leads_attempted":10
}
]

所以,键0应该插入, 键 1 应该更新,因为重复的键固定, 键 2 将插入,

但是我在键 1 上收到重复的键约束错误,因为数组映射没有等待查询执行。

const addUpdateDailyLeads = async (req, res) => {
let admin_login,category,office_id,new_leads_attempted,yyyymmdd,where,values;
let data = req.body;
req.body.map(async function(item,i){
admin_login = item.admin_login,
category = item.category,
office_id = item.office_id,
new_leads_attempted = item.new_leads_attempted,
yyyymmdd    = item.yyyymmdd;
where = {yyyymmdd:yyyymmdd, admin_login:admin_login, category:category};
values = {yyyymmdd:yyyymmdd, admin_login:admin_login, category:category,office_id:office_id,new_leads_attempted:new_leads_attempted,update_date:moment().format('YYYYMMDDHHmmss')};
console.log("calling  ",i);
let chck = await addUpdateDailyLeadsCollection({where:where,values:values})
console.log("")
console.log("called")
})
res.json({ code: '200', message: `Advisor Daily Leads Updated ${admin_login}` });
}

const addUpdateDailyLeadsCollection = async data => {
let transaction;    
let where = data.where
let values = data.values
var Sequelize = require("sequelize");
console.log("startef 1");
await AdvisorLeads.findOne({ where: where }, { useMaster: true }).then( async(data)=>{
console.log("waited");
if(data){
await data.update({new_leads_attempted: Sequelize.literal('new_leads_attempted + '+values.new_leads_attempted)}).then(data=>{
console.log("updated")
return Promise.resolve(1);
})
}else{
AdvisorLeads.create(values).then(data=>{
console.log("inserted")
return Promise.resolve(1);
})
}
})

};

控制台上的最终输出

calling   0
startef 1
waiting 1
calling   1
startef 1
waiting 1
calling   2
startef 1
waiting 1
waited
waited
waited
called
called
called
inserted
inserted
My expected output like
calling   0
startef 1
waiting 1
waited
inserted
called
calling   1
startef 1
waiting 1
waited 
updated
called
calling   2
startef 1
waiting 1
waited
inserted
called

最后我需要的是等待每个项目,执行所有查询,然后处理下一个项目

我认为您可以通过在更新中使用await来创建语句来解决。

但也要看看 UPSERT 方法,它可以大大简化您的代码。从 续集 API 参考:"插入或更新单行。如果找到与主键或唯一键上提供的值匹配的行,将执行更新。

附录:对于同步 async/await,有很多方法可以做到这一点,如这篇文章中所述。 以下是我按照 ES7 方法设置的一些代码:

let params = [{id : 1, sal : 10}, {id : 44, sal: 30}, {id : 1, sal : 20}];
async function doUpsertArrayInSequence(myParams) {
let results = [];
for (let i = 0; i < myParams.length; i++) {
let x = await User.findByPk(myParams[i].id).then(async (u) => {
if (u != null) {
await u.update({ sal : u.sal + myParams[i].sal});
} else {
await User.create({id: myParams[i].id, sal: myParams[i].sal});                    
}
});
results.push(x);
}
return results;
}
await doUpsertArrayInSequence(params).then(function(result) {
User.findAll().then(proj => {
res.send(proj);
next();
});    
})

从日志中,我可以看到 a( 选择,后跟每行的更新或插入(按顺序(。 b( id=1 的第二次出现反映了第一次出现的更新。 c( 最终查找全部反映所有插入和更新。

呵呵