事件.js:288.:无法读取 null 的属性'items'



我已经多次尝试重新启动服务器。我还删除了节点模块并重新安装了它。我还清除了缓存,并多次重新启动VS代码。似乎什么都不起作用。现在,在编写了foundList.items.push(item);之后,我也无法将项目添加到我的路由目录中。在此之前,我可以添加或删除路由目录中的项目。有人能帮我吗?

这是我的快递代码:

app.post("/", function (req, res) {
const itemName = req.body.newItem;
const listName = req.body.list;
const item = new Item({
name: itemName,
});
//Check id the user tried to add the list is default list or the custom list. Then find the custom list and add the new item and redirect to the custom list i.e ///listName
if (listName === "Today") {
item.save();
res.redirect("/");
} else {
List.findOne({ name: listName }, function (err, foundList) {
foundList.items.push(item);
foundList.save();
res.redirect("/" + listName);
});
}
});

这也是我的EJS代码。

<div class="box heading">
<h1><%= listTitle %></h1>
</div>
<div class="box">
<% newListItems.forEach(function(item) { %>
<form action="/delete" method="POST">
<div class="item">
<input
type="checkbox"
name="checkbox"
value="<%= item._id %>"
onChange="this.form.submit()"
/>
<p><%=item.name%></p>
</div>
</form>
<%});%>
<form class="item" action="/" method="post">
<input
type="text"
placeholder="newItem"
name="newItem"
autocomplete="off"
/>
<button type="submit" name="button" value="<%= listTitle %>">+</button>
</form>
</div>

这看起来像是我正在进行的Udemy培训课程。我对编码还很陌生,花了几个小时试图修复相同的代码。无论如何:

尝试更改行:foundList.items.push(item(;单数:foundList.item.prush(item(;我在代码中看不到你的listSchema。检查它,看看你是否有"项目"或"项目">
const-listSchema={name:字符串,项目:[itemsSchema]};我在架构中声明了item,但在"foundList.items.push(item("中调用items,服务器一直在崩溃。

由于listName大小写与可用的列表项不匹配,因此出现错误。需要并安装lodash并尝试使用:

const listName= _.lowerCase(req.body.list);

相关内容

最新更新