5

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.

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Keon Kim
  • 740
  • 3
  • 12
  • 27
  • Very odd that these are logged as strings yet they say they are objects. If it was a `String` object, then you'd see a bunch of other stuff other than just the string value. – Adam Jenkins Jul 08 '15 at 12:23
  • What kind of object in JavaScript logs a string value but isn't a primitive string? – Adam Jenkins Jul 08 '15 at 12:32

7 Answers7

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.

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

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()) {
    ...
}
DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
4

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

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

chrisbajorin
  • 5,993
  • 3
  • 21
  • 34
3

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

Try this:

req.user._id.toString() === post.author.user_id.toString()
friedi
  • 4,350
  • 1
  • 13
  • 19
2

Use toString() on both of the objects.

if(req.user._id.toString() === post.author.user_id.toString()) {
Tushar
  • 85,780
  • 21
  • 159
  • 179
2

Below will work for you:

req.user._id.toString() === post.author.user_id.toString()
Vikas
  • 267
  • 2
  • 10
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);

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

Community
  • 1
  • 1
Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
0

you can compare only properties by making json objects out of the objects to compare: 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

nik
  • 303
  • 3
  • 15