Cory Sparks
Cory Sparks

Reputation: 255

Mongoose referencing _id not working

This is my code for a login route, I am trying to call docs._id but it gives me back null. When I call just docs it shows this on server side:

[ { _id: EX unique id, username: { user: 'EXUsername', search: 'exusername' }, pass: 'EXpassword', email: 'EXemail' } ]

I am really confused on why I can't call docs.pass or docs._id at all but docs itself has stuff in it and I can call docs and it gives data back.

my code in node.js:

router.post('/loginUser', function(req, res){
  CheckingLogin(req.db, req.body.username, req.body.password, function(bool, docs){
    if(bool && exists(docs)){
    var err = false;
    var handleError = function(e){
    if(err){
      err = true;
      var eDoc = {};
      eDoc.errorCode = 1;
      eDoc.error = e;
      console.log(eDoc);
    }
  }
  /////problem here//////////////////////////////////
  var userID = docs._id;
  console.log(docs._id);
  if(userID != null){
    if(!err){
      tokens.db = req.db;
      tokens.refresher.create(userID, function(token){
        console.log('in token refresher ' + docs);
        res.send('1');
      });
    }else{
      console.log('Token err = true');
    }
  }else{
    console.log('Creator token = null');
    res.send('0');
  }
    }else{
        res.send('0');
    }
  });
});

Upvotes: 1

Views: 46

Answers (1)

Shumi Gupta
Shumi Gupta

Reputation: 1525

Hey its an array of object so just get it like this docs[0]._id it will work for you.

Upvotes: 1

Related Questions