Frage

so i have tried this and I have no idea why it is not working. I'm trying to console log the password of a user from the json object that mongodb returned to me. but it keeps returning undefined.

db.collection('mydb', function(err, collection) {
    collection.find({username : "john"}).toArray(function(err, items) {
        console.log(items.password);
        console.log(items["password"]);
        res.send(items);
    });
});

Both log methods return undefined and not the password that I requested. when I do console.log(items) I get the following:

[ { _id: 53599111302b055f38b95084,
    username: 'john',
    password: 'test',
    email: 'john@test.com',
    verified: 'yes',
    hash: '12345',
    session: '',
    shops: [ 0 ] } ]

So what could be the problem of this?

War es hilfreich?

Lösung

You have an array of 1 item. To access to the data you need to do this:

items[0].password

You should have a look to findOne: http://docs.mongodb.org/manual/reference/method/db.collection.findOne/

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top