Reputation: 334
Have a look
console.log(docs);
console.log(docs.key);
console.log(docs.user);
// outputs:
[ { key: 'HunueVwerwbZwesZHxntesDciakecyiJ',
user: '[email protected]',
createdAt: Mon Mar 17 2014 08:48:30 GMT-0400 (EDT),
_id: 5326ef1ee883062522faa4a8,
__v: 0 } ]
undefined
undefined
What's wrong with the way I am trying to access this object?
Upvotes: 1
Views: 51
Reputation: 239443
docs
is an Array. You can check that like this
console.log(Array.isArray(docs));
// true
So, you can check the length of the array, like this
console.log(docs.length);
// 1
Since it has only one element, we can access the first element with the subscript notation, like this
console.log(docs[0].key);
console.log(docs[0].user);
Note: We access the first element with 0
, because JavaScript Arrays start with index 0.
Instead you can drop the object and retain only the array, like this
docs = docs[0];
Upvotes: 3