I'm trying to add new items in my mongoDB database with the "app.post" in my "Today" list.
this is my root route with the list title - Today.
app.get("/", function (req, res) {
Item.find({}, (err, foundItems) => {
if (foundItems.length === 0) {
Item.insertMany(defaultItems, (err) => {
if (err) {
console.log(err);
}
});
res.redirect("/");
} else {
res.render("list", { listTitle: "Today", newListItems: foundItems });
}
});
});
Posting a new item in my "Today" list logs the else-statement in my console, even though both logs at the end show "Today".
app.post("/", function (req, res) {
const newItem = req.body.newItem;
const listName = req.body.list;
const item = new Item({
name: newItem,
});
if (listName === "Today") {
item.save();
res.redirect("/");
} else {
console.log("Doesn't Exist");
}
console.log(listName);
console.log("Today");
});
Hope this snippet is helpful to solve the problem.
Aucun commentaire:
Enregistrer un commentaire