I have the issue with using promises and async calls from Mongoose
Here is my code
req.checkBody(BookManager.SCHEME);
req.getValidationResult()
.then(function (result) {
if (!result.isEmpty()) {
handler.onError('Invalid payload');
return;
}
return new BookModel({
author: data.author,
name: data.name,
year: data.year
});
})
.then((book) => {
BookModel.find({name: book.name}, function (err, docs) {
if (docs.length) {
throw new Error("Book already exists");
} else {
return book;
}
});
})
.then((book) => {
book.save();
})
.then((saved) => {
handler.onSuccess(saved);
})
.catch((error) => {
handler.onError(error.message);
});
As you can see from the code above. I am checking whether such book already exists and in order to do this I use async find method that is called after the main "program counter" has gone further.
How can I solve this problem ?
Also please tell me whether I have chosen the right wait to implement my use case ? Maybe I am doing it wrong and there are some other best practices to handle this.