Consider:
app.get("/posts/:postId", function(req, res) {
const requestedPostId = req.params.postId;
Post.findOne({_id: requestedPostId}, function(err, post) {
res.render("post", {
title: post.title,
content: post.content
});
});
});
This is what used to work for me, using Express.js and Mongoose. How can I fix it?
3
Answers
I got the solution thanks to my friend @Sean. Instead of the callback function, I have to replace it with a .then() function:
MongoDB has removed callbacks from its Node.js driver as of version 5.0. See findOne.
If you really need to use callbacks instead of promises, you will need to use an older version of the driver.
In version
5.0
, MongoDB has removed callbacks from itsnode.js
driver. So now you can leverage thePromise
instead of callback.Updated Code:
Also Refer: https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#findOne