我用express写了一个Node api。在此api中,我将从数据库处理中获取一些数据,并将最终数据作为api负载发送。
这是我的代码。
exports.getAllAco = (req, res) => {
let acoList = [], i = 0;
let condaions = {
deleted: 0,
}
let attributes = ['id', 'name', 'email', 'type', 'phone', 'gender', 'main_sys_id', 'avatar', 'in_service', 'created_by']
Methods.getAllData(User, condaions, attributes).then((userList) => {
userList.map( user => {
i = i + 1;
if (user.dataValues.type == 'aco') {
let condations = {
aquisition_member_id: user.dataValues.id,
deleted: 0
}
Methods.getDetailsFromTwoAssociateTable(condations, Task, DetailTask).then(tasks => {
let x = {
user: user,
tasks: tasks
}
x.user.dataValues.totalTask = tasks.length;
let index = 0;
x.user.dataValues.userTotalAssigned = 0, x.user.dataValues.userTotalCalled = 0, x.user.dataValues.userTotalConverted = 0, x.user.dataValues.userTotalRejected = 0;
tasks.map(v=>{
x.tasks[index].dataValues.totalAssignedCustomer = v.dataValues.detail_tasks.length;
x.tasks[index].dataValues.totalCalled = 0;
x.tasks[index].dataValues.totalConverted = 0;
x.tasks[index].dataValues.totalRejected = 0;
v.dataValues.detail_tasks.map(detail_task=>{
x.user.dataValues.userTotalAssigned += 1;
if(detail_task.dataValues.phone_call_status == 'called' ){
x.tasks[index].dataValues.totalCalled += 1;
x.user.dataValues.userTotalCalled +=1;
}
else if(detail_task.dataValues.phone_call_status == 'confirmed'){
x.tasks[index].dataValues.totalConverted +=1;
x.user.dataValues.userTotalConverted += 1;
}
else{
x.user.dataValues.userTotalRejected += 1;
x.tasks[index].dataValues.totalRejected += 1;
}
})
index=index+1;
})
// x.user.dataValues.totalAssigned = totalAssigned;
acoList.push(x)
})
}
})
return acoList;
}).then(acoList=>{
setTimeout(() => {
Methods.successResponse(req, res, acoList)
}, 2000);
}).catch((error) => {
ErrorResMethods.errorResponse(req, res, error);
})
}
要发送实际结果,我必须等待两秒钟,否则响应为空。
如何使用async / await实现它。
答案 0 :(得分:0)
我想类似的事情应该可以完成。
exports.getAllAco = async (req, res) => {
let i = 0;
const condaions = {
"deleted": 0
};
const attributes = ["id", "name", "email", "type", "phone", "gender", "main_sys_id", "avatar", "in_service", "created_by"];
const userList = await Methods.getAllData(User, condaions, attributes);
const acoList = await Promise.all(userList.map(async user => {
i += 1;
if (user.dataValues.type == "aco") {
const condations = {
"aquisition_member_id": user.dataValues.id,
"deleted": 0
};
const tasks = await Methods.getDetailsFromTwoAssociateTable(condations, Task, DetailTask);
const x = {
"user": user,
"tasks": tasks
};
x.user.dataValues.totalTask = tasks.length;
let index = 0;
x.user.dataValues.userTotalAssigned = 0, x.user.dataValues.userTotalCalled = 0, x.user.dataValues.userTotalConverted = 0, x.user.dataValues.userTotalRejected = 0;
tasks.forEach(v => {
x.tasks[index].dataValues.totalAssignedCustomer = v.dataValues.detail_tasks.length;
x.tasks[index].dataValues.totalCalled = 0;
x.tasks[index].dataValues.totalConverted = 0;
x.tasks[index].dataValues.totalRejected = 0;
v.dataValues.detail_tasks.map(detail_task => {
x.user.dataValues.userTotalAssigned += 1;
if (detail_task.dataValues.phone_call_status == "called") {
x.tasks[index].dataValues.totalCalled += 1;
x.user.dataValues.userTotalCalled += 1;
} else if (detail_task.dataValues.phone_call_status == "confirmed") {
x.tasks[index].dataValues.totalConverted += 1;
x.user.dataValues.userTotalConverted += 1;
} else {
x.user.dataValues.userTotalRejected += 1;
x.tasks[index].dataValues.totalRejected += 1;
}
});
index += 1;
});
return x;
}
})
Methods.successResponse(req, res, acoList);