【发布时间】:2021-05-28 16:42:15
【问题描述】:
like 不会从 post 模型中删除
likeSchema
.findOne({$and: [{post: postId ,user: userId}]})
.exec((err, result) =>{
if (result) {
db.likeSchema
.findOne(
{
$and: [
{ post: postId },
{ user: userId }
]
}
)
.exec((err, like) => {
db.likeSchema
.findOneAndUpdate(
{
$and: [
{ post: postId },
{ user: userId }
]
}
)
.remove()
.exec(() => {
db.postSchema
.findOneAndUpdate(
{ _id: postId.postID },
{
$pull: { 'likes': like._id }
}
)
.exec((err, post) => {
})
})
})
}
const likeSchema = new schema({
post: { type: schema.Types.ObjectId, ref: "postSchema" },
user: { type: schema.Types.ObjectId, ref: "userSchema" }
})
const postSchema = new schema({
owner: { type: schema.Types.ObjectId, ref: "userSchema" },
text: String,
image: String,
comments: [{ type: schema.Types.ObjectId, ref: "commentSchema" }],
likes: [{ type: schema.Types.ObjectId, ref: "likeShcema" }],
date: { type: Date, default: Date.now }
});
【问题讨论】:
-
请提供
LikeSchema和PostSchema -
const likeSchema = new schema({ post: { type: schema.Types.ObjectId, ref: "postSchema" }, user: { type: schema.Types.ObjectId, ref: "userSchema" } , }) const postSchema = new schema({ owner: { type: schema.Types.ObjectId, ref: "userSchema" }, text: String, image: String, cmets: [{ type: schema.Types.ObjectId, ref: "commentSchema" }], 喜欢:[{ type: schema.Types.ObjectId, ref: "likeShcema" }], date: { type: Date, default: Date.now } });
标签: javascript node.js mongodb mongoose nosql