5
console.log('>>>>>>user = '+ user);

outputs

>>>>>>user      = { username: 'user1',
  salt: '3303187e50a64889b41a7a1c66d3d3c10b9dec638fdd033bee1221d30d01c5e1',
  hash: 'a174c206d88bee1594bb081dbd32d53420f6ef3d6322104f3d0722d58bc8dd8d',
  _id: 52d3177481daf59c11000001,
  __v: 0 }

but

console.log('>>>>>>user.hash = '+ user.hash);

outputs

>>>>>>user.hash = undefined

What could be causing this?


Edit: Interestingly, user._id, (and only it) works.

Community
  • 1
  • 1
laggingreflex
  • 32,948
  • 35
  • 141
  • 196

3 Answers3

4

update: Solved in mongoose v3.8.19


It's totally a mongoose issue.

A solution was to not go schema-less. I was using strict: false when defining my schema (for making my database schema-less)

var Users = mongoose.model('Users', new mongoose.Schema({
    },{strict:false}));

Adding hash here solved it.

var Users = mongoose.model('Users', new mongoose.Schema({
    hash: String
    },{strict:false}));
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
1

I believe user is an object and to access object property in JS you use . notation(Property Accessors).

And In a code posted above I can see ,ID field is missing 'quotes',

user = { username: 'user1',
         salt: '3303187e50a64889b41a7a1c66d3d3c10b9dec638fdd033bee1221d30d01c5e1',
         hash: 'a174c206d88bee1594bb081dbd32d53420f6ef3d6322104f3d0722d58bc8dd8d',
         _id: '52d3177481daf59c11000001',
         __v: 0 
     };

console.log(user.hash); // accessing hash property of `user` Object.

Working fiddle

Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79
0

Taking a hint from this answer, can you try changing your findOne arguments as:

function(username, password, done){
    Users.findOne({username:new require('mongodb').ObjectID(username)}, function(err, user) {
        // do something here
    });
}

Note: {username:new require('mongodb').ObjectID(username)}

Basically, you need to construct the 'username' and not pass it as a string to findOne.

Community
  • 1
  • 1
Vikram Deshmukh
  • 12,304
  • 4
  • 36
  • 38