下面的代码是我在Youtube视频中找到的一个4年前的例子。
TypeError: Cannot read properties of undefined (reading 'push')
有人能弄清楚新语法是什么吗?
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
const Schema = mongoose.Schema;
const commentSchema = new Schema({
text: String,
username: String,
});
const postSchema = new Schema({
text: String,
username: String,
comments: [commentSchema],
});
const PostModel = mongoose.model('post_coll', postSchema);
const CommentModel = mongoose.model('comment_coll', commentSchema);
const aPost = new PostModel({
text: 'one',
username: 'two',
});
aPost.comment.push({
text: 'one',
username: 'two',
});
aPost.save((err, res) => {});
2条答案
按热度按时间gr8qqesn1#
您应该将
push
更改为comments
:vc9ivgsu2#
如果我没猜错的话,是这样做的,让我知道这是否有效,谢谢。