我这里有一段简单的代码。
它的意图是验证用户与发布帖子的用户是否相同,并允许已验证的用户编辑该帖子。
exports.edit = function(req, res){
Post.findById(req.params.post_id, function(err, post){
if(err){
return res.json({
type:false,
message:"error!"
});
}else if(!post){
return res.json({
type:false,
message:"no post with the id"
})
}else{
console.log(req.user._id, typeof req.user._id);
console.log(post.author.user_id, typeof post.author.user_id);
if(req.user._id === post.author.user_id){ // doesn't work!!
return res.json({
type:false,
message:"notAuthorized"
});
}else{
return res.json({
type:true,
message:"it works",
data:post
});
}
}
});
}
控制台显示:
557c6922925a81930d2ce 'object'
557c6922925a81930d2ce 'object'
这意味着它们在价值和类型上是相等的。我也试过使用“==”,但那也不起作用。我怀疑需要做一些比较对象的操作,但我不确定应该做什么。