问题描述
这是需求代码dataJoin(对象数组,如多个记录)的结果,具有json值。这些json值,必须找到相关的项目。这样,用于语句,用于查找json值。使用userService函数收集json值,并获取数据,然后将这些数据合并为父数据。没用因为在完成for循环语句之前,已执行res.send(“ ...”)项目,所以这是我的问题...
请任何人指导我...
谢谢
table1Obj
.findAll(
{
include: [
{
model: assocaitonTableObject,required: false,}
],where: req.query,})
.then((dataJoin) => {
for(element of dataJoin ) {
const arrayCollection = [];
let whereConstraint = {};
JSON.parse(element.joinedUsersList).forEach( async (ele) => {
arrayCollection.push(ele.userId);
});;
whereConstraint = {
id : {
[Op.in]: arrayCollection
}
}
const usesrListData = userService.customfindAll(whereConstraint);
element.userListData = usesrListData;
}
res.send({
status:200,message:"OK",data:dataJoin
});
})
.catch((err) => {
res.status(500).send({
status:500,message:
err.message,});
});
解决方法
我的第一个猜测是您的userService.customfindAll
返回了Promise,那么您应该等待它。如果不是,请检查是否已等待每个Promise。
我的第二个猜测是您不应该为array.forEach
传递异步函数,请尝试使用for
。我不确定这是否有帮助,但是您可以尝试。