Answer the question
In order to leave comments, you need to log in
How to execute multiple queries to mongodb and return only after all are completed?
I need to execute several queries .countDocuments
to the database, update the counter in each of them, and make return
this counter.
I tried to do async / await, but for some reason it works every other time. One time the .then
updated counter comes in, another time the standard 0 comes in.
Here's how I did it:
const countBookedAndOrderedBook = async () => {
let bookedAndOrderedBooksArr = 0;
await BookedBooks.countDocuments(
{ userId },
(bookedBooksrr, countBookedBooks) => {
if (bookedBooksrr) throw bookedBooksrr;
console.log({ countBookedBooks });
bookedAndOrderedBooksArr += countBookedBooks;
}
);
return bookedAndOrderedBooksArr;
};
countBookedAndOrderedBook().then(count => {
console.log(
{ count },
servConf.maxBooksPerOneUserBookedAndOrderedAtTheSameTime
);
...
console.log(count)
it, the count will be 0, and if you output it to the console by timeout, then everything is fine, but the timeout is nonsense. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question