Express 后授权不';行不通

Express 后授权不';行不通,express,mongoose,mean-stack,Express,Mongoose,Mean Stack,我试图限制一个用户编辑其他人帖子的能力。 当我点击“编辑帖子”时,它会更改帖子的作者,是因为“保存”方法还是? 以下是我的尝试: router.put('/posts/:id', passport.authenticate('jwt'), (req, res) => { Post.findOne({_id: req.params.id}, (err, post) => { if (err) throw err; if (post.author = req.user.

我试图限制一个用户编辑其他人帖子的能力。 当我点击“编辑帖子”时,它会更改帖子的作者,是因为“保存”方法还是? 以下是我的尝试:

router.put('/posts/:id', passport.authenticate('jwt'), (req, res) => {

Post.findOne({_id: req.params.id}, (err, post) => {
    if (err) throw err;
    if (post.author = req.user._id) {
        post.title = req.body.title,
        post.content = req.body.content,
        post.postImageUrl = req.body.postImageUrl

        post.save((err, updatedPost) => {
            if (err) throw err;
            else {
                res.json({message:'You have successfully updated your post', success: true});
            }
        });

    } else {
        res.json({success: false, message: 'You are not allowed to do this.'});
    }
});
}))


我检查了post.author和req.user.\u id,它们匹配

请检查您的条件,以检查当前用户是否是文章的作者。您尝试分配和更改作者值,而不是检查是否相等

if(post.author=req.user.\u id)

解决方案:

Mongoose提供了一种检查对象ID是否相等的
.equals
方法。以下是Mongoose使用的for mongodb本机驱动程序
.equals
方法的链接


if(post.author.equals(req.user.\u id))

如果(post.author=req.user.\u id)be
if(post.author==req.user.\u id)
?这是个问题,但现在它返回的是作者和用户不匹配,虽然它们是..它们都是字符串吗?你能给出一个值的示例吗?
post.author
是一个参考,
req.user.\u id
是一个对象属性。我强烈怀疑post.author和req.user.\u id可能代表不同的东西。您能否通过发布存储在mongo中的post和用户文档的示例来详细说明您的问题。