{当我评论Like和Comment模式时,我的代码运行良好,否则由于comment和like模式而显示错误。我正在创建一个演示项目,在其中创建帖子并向其中添加“喜欢评论”按钮,以便观众可以喜欢和评论}
我拥有的post.js文件中
const mongoose = require("mongoose");
const postSchema = mongoose.Schema({
content: { type: String, required: true },
imagePath: { type: String, required: true },
creator: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true,
},
});
const LikeSchema = mongoose.Schema({
likedByUser: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true,
},
});
const CommentSchema = mongoose.Schema({
comment: { type: String, required: true },
creator: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true,
},
});
module.exports = mongoose.model("Post", postSchema);
module.exports = mongoose.model("Like", LikeSchema);
module.exports = mongoose.model("Comment", CommentSchema);
在user.js文件中,我有
const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");
const userSchema = mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
});
userSchema.plugin(uniqueValidator);
module.exports = mongoose.model("User", userSchema);