【发布时间】:2017-05-03 09:51:23
【问题描述】:
我有一个 objectIds 列表,我想转到不同的集合并根据每个 Id 进行操作。我宁愿一个接一个地进行操作(按顺序)
var removeOperation = function(objectified){
return Comps.findOne({reviews : objectified}).populate([{ path: "reviews", match : {_id : objectified}}])
}
var firstCheckIfAnonHasTheIdInReviewsArrayIfThereDeleteIt = function(objectified){
var query = {reviews : objectified};
var update = {$pull : {reviews : objectified}};
var option = {new :true};
return Anon.findOneAndUpdate(query, update, option );
};
var thenCheckIfUserHasTheIdInReviewsArrayIfThereDeleteIt = function(objectified){
var query = {reviews : objectified};
var update = {$pull : {reviews : objectified}};
var option = {new :true};
return User.findOneAndUpdate(query, update, option );
}
我正在走这条路:
Promise.mapSeries(arrOfObjectIds, function(e){
return removeOperation(e);
})
.then(function(results){
console.log(results);
var map = results.map(function(e){
// return e.reviews[0]
return e
})
console.log("map : ", map)
return Promise.resolve(map);
})
.then(function(compDocs){
console.log("compDocs: ",compDocs)
Promise.mapSeries(compDocs, function(compDoc){
return updateCompAndRemoveReviewFromArray(compDoc) // I know it's not show. It's another promise I use
})
}).then(function(returned){
return Reviews.remove({_id : {$in : arrOfObjectIds }})
})
.then(function(){
I wanted to do firstCheckIfAnonHasTheIdInReviewsArrayIfThereDeleteIt on the array of object Ids to delete the review from the array. Also if we succesfully removed the array here we should not have to go to the next user
promise which deletes a users review since if we deleted in Anon it won't be in User. since there is only one review ID possible per review.
})
.then(function(){
//if there was no review pulled from the Anon reviews Array. that means it's in the users review and we should do this promise
thenCheckIfUserHasTheIdInReviewsArrayIfThereDeleteIt()
})
所以也许你可以告诉我如何在一组元素上使用mapSeries,这样它就不会做出一个承诺,而是做出多个承诺。
我们可以这样做吗:
Promise.mapSeries(arrOfObjectIds, function(e){
return removeOperation(e);
return firstCheckIfAnonHasTheIdInReviewsArrayIfThereDeleteIt(e)// extra credit: check if this was successful (review was pulled). If it wasn't got to next one.
return thenCheckIfUserHasTheIdInReviewsArrayIfThereDeleteIt(e)
})
【问题讨论】:
标签: javascript node.js mongoose promise bluebird