为什么Javascript比较不能与对象一起使用? [重复]

时间:2022-09-25 14:12:19

This question already has an answer here:

这个问题在这里已有答案:

I have simple code here.

我这里有简单的代码。

The intention of it is to verify the user with the user who wrote the post and allow the verified user to edit the post.

其目的是用编写帖子的用户验证用户并允许经过验证的用户编辑帖子。

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
                }); 
            }
        }
    });
}

The console says:

控制台说:

557c6922925a81930d2ce 'object'
557c6922925a81930d2ce 'object'

Which means they are equal in value and also equal in types.

这意味着它们在价值上是相等的,在类型上也是相同的。

I tried with == too, but that also doesn't work.

我也试过==,但这也行不通。

I am suspecting there needs to be something done to compare objects, but I don't know exactly what I should do.

我怀疑需要做些什么才能比较对象,但我不确切知道应该做些什么。

7 个解决方案

#1


6  

Javascript, when asked to compare two object, compare the address of the object, not the object himself. So yes, your objects have the same value, but are not in the same place in memory.

Javascript,当被要求比较两个对象时,比较对象的地址,而不是对象本身。所以是的,你的对象具有相同的值,但在内存中不在同一个位置。

You can try to extract the id in new variable and compare that (or convert them to string and compare the strings).

您可以尝试在新变量中提取id并对其进行比较(或将它们转换为字符串并比较字符串)。

Examples:

var id_edit = req.user._id,
    id_post = post.author.user_id;
if (id_edit === id_post) {
    //...
}

Or

if(req.user._id.toString() === post.author.user_id.toString()) {
    ...
}

#2


4  

People have mentioned toString(), but mongo also has it's own methods for ObjectIds. You can use:

人们提到了toString(),但是mongo也有自己的ObjectIds方法。您可以使用:

post.author.user_id.equals(req.user._id)

#3


3  

You have to compare the string representations of the mongodb identifier objects.

您必须比较mongodb标识符对象的字符串表示形式。

Try this:

req.user._id.toString() === post.author.user_id.toString()

#4


2  

Use toString() on both of the objects.

在两个对象上使用toString()。

if(req.user._id.toString() === post.author.user_id.toString()) {

#5


2  

Below will work for you:

以下将适合您:

req.user._id.toString() === post.author.user_id.toString()

#6


2  

This question has already been answered here, but for the purposes of being explicit...

这个问题已在这里得到解答,但为了明确......

If you're using underscore, you can just do _.isEqual(obj1, obj2);

如果你使用下划线,你可以只做_.isEqual(obj1,obj2);

However, there is no good general way of doing this. Read the other similar questions on SO for details.

但是,没有一般的方法可以做到这一点。有关详细信息,请阅读SO上的其他类似问题。

#7


0  

you can compare only properties by making json objects out of the objects to compare: JSON.stringify(obj1) === JSON.stringify(obj2)

您只能通过从要比较的对象中生成json对象来比较属性:JSON.stringify(obj1)=== JSON.stringify(obj2)

though it is not going to check methods and types of the properties, it is simplest and probably fastest comparison to implement

虽然它不会检查属性的方法和类型,但它实现起来最简单且可能是最快的比较

#1


6  

Javascript, when asked to compare two object, compare the address of the object, not the object himself. So yes, your objects have the same value, but are not in the same place in memory.

Javascript,当被要求比较两个对象时,比较对象的地址,而不是对象本身。所以是的,你的对象具有相同的值,但在内存中不在同一个位置。

You can try to extract the id in new variable and compare that (or convert them to string and compare the strings).

您可以尝试在新变量中提取id并对其进行比较(或将它们转换为字符串并比较字符串)。

Examples:

var id_edit = req.user._id,
    id_post = post.author.user_id;
if (id_edit === id_post) {
    //...
}

Or

if(req.user._id.toString() === post.author.user_id.toString()) {
    ...
}

#2


4  

People have mentioned toString(), but mongo also has it's own methods for ObjectIds. You can use:

人们提到了toString(),但是mongo也有自己的ObjectIds方法。您可以使用:

post.author.user_id.equals(req.user._id)

#3


3  

You have to compare the string representations of the mongodb identifier objects.

您必须比较mongodb标识符对象的字符串表示形式。

Try this:

req.user._id.toString() === post.author.user_id.toString()

#4


2  

Use toString() on both of the objects.

在两个对象上使用toString()。

if(req.user._id.toString() === post.author.user_id.toString()) {

#5


2  

Below will work for you:

以下将适合您:

req.user._id.toString() === post.author.user_id.toString()

#6


2  

This question has already been answered here, but for the purposes of being explicit...

这个问题已在这里得到解答,但为了明确......

If you're using underscore, you can just do _.isEqual(obj1, obj2);

如果你使用下划线,你可以只做_.isEqual(obj1,obj2);

However, there is no good general way of doing this. Read the other similar questions on SO for details.

但是,没有一般的方法可以做到这一点。有关详细信息,请阅读SO上的其他类似问题。

#7


0  

you can compare only properties by making json objects out of the objects to compare: JSON.stringify(obj1) === JSON.stringify(obj2)

您只能通过从要比较的对象中生成json对象来比较属性:JSON.stringify(obj1)=== JSON.stringify(obj2)

though it is not going to check methods and types of the properties, it is simplest and probably fastest comparison to implement

虽然它不会检查属性的方法和类型,但它实现起来最简单且可能是最快的比较