Good morning. I hope You are well.So i try finish some udemy course but i stuck with this code because mogoose no longer accepts callback function.I tried to change function to :"then"+".catch", but still not working.
app.get("/", function (req, res) {
Item.find({}, function (err, foundItems) {
if (foundItems.length === 0) {
Item.insertMany(defaultItems, function (err) {
if (err) {
console.log(err);
} else {
console.log("Successfully savevd default items to DB.");
}
});
res.redirect("/");
} else {
res.render("list", { listTitle: "Today", newListItems: foundItems });
}
});
});
and error form terminal:
"MongooseError: Model.find() no longer accepts a callback"
Sorry to take up your time, I know you have better things to do.
3
Answers
Instead of passing a callback, Monggose methods are now returning
Promise
s. So you should go for something like thisI’m new here..sorry if not a good enough answer…but
You’re gonna have to use the async/await focntionnality…
You have to enclose :
in a async fonction… and call it with await.
Here is an example of my code :
then you call :
Heyy, i’m taking the same course and that’s how i did it :
in newer version of moongose a couple of things are different.
await MyModel.find({});
needs to be inside a async function in order to work I called itmyitems()
like in the snippet above.