1

Hi, to explain my app briefly: I have a User Schema and a "Tweet" Schema (Actually it's a 'Post' Schema but to avoid confusion with the 'post' method let's call it a "Tweet Schema').

User's have many "tweets" , but a "tweet" only has one User (like twitter). Inside my tweet schema I have a key of Original_Poster, which is a reference to the id of the User who created the post (see below for schema).

In a route to update a tweet, I have an if else statement to confirm if the tweet that the user is trying to update, is actually their tweet. This is functioning while using locally created users with mongodb/mongoose, but not with users created through passport-facebook.

Here is the if else statement:

        if(req.user._id == post.original_poster){
            console.log('nice')
        }else{
            console.log(req.user._id, post.original_poster);
            console.log(typeof req.user._id, typeof post.original_poster)
            console.log('wtf')
        }

What I expect to see is in my console is: 'nice'

What I am actually seeing is:

5a288913fa0d78257fd7b4d3 5a288913fa0d78257fd7b4d3
object object
wtf

Clearly they are the same user ids, and the type is the same (though this shouldn't matter as I am using the double equals not the triple equals).

Can anyone explain this?

Edit: Here is the full Post.find({}) for more info:

Post.findOne({_id: req.params.id}).then((post)=>{
    if(req.user._id == post.original_poster){
        console.log('nice')
    }else{
        console.log(req.user._id, post.original_poster);
        console.log(typeof req.user._id, typeof post.original_poster)
        console.log('wtf')
    }
})
Nandu Kalidindi
  • 6,075
  • 1
  • 23
  • 36
mdash1
  • 1,115
  • 4
  • 17
  • 31
  • `console.log(post.original_poster, typeof post.original_poster); console.log(req.user._id,typeof req.user._id)` here trying to get `typeof` so it returns `object` – Kalaiselvan Dec 07 '17 at 02:27
  • and here `console.log('req.user._id == req.user._id',req.user._id == req.user._id)` from the request only your comparing like `(1==1)` so it returns true – Kalaiselvan Dec 07 '17 at 02:28
  • Sorry, that is a typo, let me fix my question and show you that the results are still strange even if I fix the typo – mdash1 Dec 07 '17 at 02:35
  • `{foo: 3} !== {foo: 3}`. They are not the same object. Neither are your objects. If they have the same string representation then compare those. – Jared Smith Dec 07 '17 at 02:42
  • You are comparing objects from the `type of` console.log. Which will not be equal. – Nandu Kalidindi Dec 07 '17 at 02:46

1 Answers1

-1

Figured it out, though the answer is a bit weird to me, if anyone can explain why this works, please feel free to explain below

I changed if(req.user._id == post.original_poster) to if(String(req.user._id) == post.original_poster) and now 'nice' is being console.logged. Though this is a bit strange to me because I am using the == double equals not the === so the types of the things under comparison should not be considered.

if(String(req.user._id) == post.original_poster){
                console.log('nice')
            }else{
                console.log(req.user._id, post.original_poster);
                console.log(typeof req.user._id, typeof post.original_poster)
                console.log('wtf')
            }
mdash1
  • 1,115
  • 4
  • 17
  • 31
  • Whoever downvoted this, can you explain why you did so, this way I can adjust my answer. – mdash1 Dec 07 '17 at 02:45
  • 1. It doesn't actually answer your question, it should be an update edit to your question. And you know this, or you wouldn't ask for an explanation of your own answer. 2. Your question is a duplicate and should be closed. – Jared Smith Dec 07 '17 at 02:46