[英]nodejs Promise.all returns null on some mongoose promises
i am passing array of mongoose promises and for some reason sometimes customerFindPromise return null, and sometimes it return the requested object. 我传递的是猫鼬的诺言数组,由于某种原因,有时customerFindPromise返回null,有时返回所请求的对象。 am i doing something wrong here
我在这里做错什么吗
******************** START result0: null result END ********************
******************** START result1: { _id: 5a18a637346826574416a588,
doc_fname: 'Bob',
doc_lname: 'Smith',
numOfCases: 1,
__v: 0 } result END ********************
******************** START result2: {"n":1,"ok":1} result END ********************
Code 码
var customer = {
doctor_id: 5a18a637346826574416a588,
cust_fname: 'dfsdf',
cust_lname: 'sdasd',
case_type: 'Crowns',
_id: 5a1cd19438f14164b0087753
}
test(customerCase);
async function test(customerCase) {
console.log("******************** customerCaseDelete: "+ customerCase._id +" ********************");
var _id = customerCase._id;
var doctor_id = customerCase.doctor_id;
var query = {_id:_id};
const customerFindPromise = CustomerCases.findById(_id);
const customerRemovePromise = CustomerCases.remove(query);
const doctorUpdatePromise = Doctors.findOneAndUpdate({_id:doctor_id},{'$inc': {'numOfCases': -1}},{new:true});
await Promise.all([customerFindPromise,doctorUpdatePromise,customerRemovePromise])
.then((result) => {
console.log("******************** START result0: "+ result[0] +" result END ********************");
console.log("******************** START result1: "+ result[1] +" result END ********************");
console.log("******************** START result2: "+ result[2] +" result END ********************");
res.json(result);
}).catch((err) => {
console.log("******************** START err: "+ err +" err END ********************");
throw err;
});
}
The functions you are running are asynchronous and won't always run in order. 您正在运行的功能是异步的,并非总是按顺序运行。
Promise.all
does not change the execution order of the promises either, it just waits for them all to be resolved. Promise.all
也不会更改Promise.all
的执行顺序,它只是等待它们全部解决。
Sometimes CustomerCases.remove
will execute and resolve before CustomerCases.findById
so null
resolves when the document has been removed first. 有时,
CustomerCases.remove
将在CustomerCases.findById
之前执行并解析,因此null
在首次删除文档时解析。
Wait for the promises to resolve serially before executing the next query, seeing as you are using async
/ await
it's fairly simple to add: 在执行下一个查询之前,等待promise依次解析,请参见使用
async
/ await
,添加起来相当简单:
try{
let find = await CustomerCases.findById(_id)
console.log(`find: ${find}`)
let update = await CustomerCases.remove(query)
console.log(`update: ${update}`)
let remove = await Doctors.findOneAndUpdate({_id:doctor_id},{'$inc': {'numOfCases': -1}},{new:true})
console.log(`remove: ${update}`)
res.json([find, update, remove])
} catch(err) {
console.error(err)
throw err // next(err)?
}
Try bluebirds Promise.each
if you need a serial execution helper. 如果需要串行执行助手,请尝试bluebirds
Promise.each
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.