I query author data from a MongoDB via Mongoose (MEAN environment). The author data also contains an array of books that the author wrote (-> results.books). Once received, I'd like to iterate through this array of books and check for certain values. This is my code so far.
return Author.findOne({_id: req.user._id}, '-username').execAsync()
.then(function(results) {
return Promise.each(results.books) //this line causes TypeError rejection
}).then(function(book){
console.log('book:'+book); // test output
if(book==='whatever‘){
//do foo
}
}).catch(function(err){
console.log('Error: '+err);
});
Unfortunately I can't get it to work as it keeps giving me a rejection TypeError for the line marked above. I tried to apply this solution here (Bluebird Promisfy.each, with for-loops and if-statements?) but it wouldn't work out as it also seems to be a different kind of problem.