node.js - “TypeError:使用 mongoose ODM 时无法读取未定义的属性‘push’”
问题描述
下面是我收到上述错误的代码片段。基本上我想使用 id 在我的集合中找到主题,然后将评论推送到它(数组)。我在我的项目中使用了猫鼬模型。
Topic.findById(id)
.then((result) => {
console.log(result);
topic = result,
topic.comments.push(comm);
topic.save()
.then((result) => {
res.redirect("/topics/:id");
})
.catch((err) => {
console.log(err);
});
})
我还尝试了其他我认为更不正确的选项。如果有帮助,我会将它们写在评论中。:)
解决方案
在我的 TopicSchema 中,我刚刚启动了下面的评论数组,它起作用了!
comments: {
type: Array,
required: false
}
推荐阅读
- jupyter-notebook - 更改 Jupyterlab 启动文件夹 (MacOS: Post Hurricane Catalina)
- javascript - .drawImage 函数为画布抛出“TypeError: Image or Canvas expected”
- linux - 使用 awk 在 /etc/passwd 中显示重复的 GID
- bash - 计算文件 test.txt 中有多少个以“tol”开头的单词?
- javascript - javascript 正则表达式验证以匹配特定模式以从文本框中接受
- flutter - 如何比较用户输入和 sqflite 中的记录
- java - 在链表中添加节点时陷入无限循环
- android - 如何在设备中自动安装应用程序
- machine-learning - 如何从 pytorch 预训练的微调模型中提取特征
- google-drive-api - 我们如何将驱动器更改循环到 pageToken?