【发布时间】:2020-03-19 16:34:41
【问题描述】:
我试图使用 async/await 功能,但我的问题是我有一个 async 函数,我需要从中和内部返回一些数据我嵌套了 3 个 await 函数,其中最后一个返回整个函数的值,但我不能这样做,因为最后一个等待是来自 mongodb 的查找查询,它只返回找到的元素。所以我想知道是否有某种方法可以从该 await 函数获取该数据。
async register_Employee_Credential (id,req,res){
try{
let employee_credential= new Employee_credential({
employee: id,
username: req.body.username,
password: req.body.password
});
await bcrypt.genSalt(10,async (err,salt)=>{ //first await function
await bcrypt.hash(employee_credential.password,salt, async (err,hash)=>{ //second await function
if(err) console.log("error while generating salt");
employee_credential.password = hash;
result = await Employee_credential.create(employee_credential,async (err,result)=>{ // third await function
if(err)
{
var errMessage = await help.Property_Validator(err);
return errMessage; // this is the return message i need
}
})
})
})
return errMessage; //this is the final return for the calling function
}catch(err){
console.log("employee creditial error furthur: " + err);
}
}
【问题讨论】:
标签: node.js asynchronous