У меня есть список объектов, и я хочу перейти в разные коллекции и сделать базу операций на каждом 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)
})