try {
const review = await Review.create({
user: user.userid,
place_id: pid,
stars: stars,
content: content,
});
/* await */ User.findByIdAndUpdate(user.userid, {
$push: {reviews: review._id}
});
res.status(200).json(returnResponse(false, "reviewWritten", "-"));
return;
} catch (error) {
console.error(error);
res.status(500).json(returnResponse(true, "errorAtReviewWriting", "-"));
return;
}
I have written the code as shown above.
I thought that since I don’t need the return value of User.findByIdAndUpdate, I don’t have to use the await keyword and let it run asynchronously. Why does it only work correctly when I use the await keyword?
When I don’t use the await keyword, It’s not updated at all. Literally.
I have verified that the data has not been edited at all by findById() method after saving.
And, error didn’t occur since the returned response code was 200.