I am trying to read a from Mongo DB and assign a value to a variable . Does not work.
var collection = db.get('accesstoken');
collection.find({},{},function(e,docs){
console.log(docs);
var global_token_var = docs.toArray()[0].access_token;
console.log(" global token variable set to " + global_token_var);
});
Output as below
[ { _id: 594aaa17f0db330e70bd6e89, access_token: '76c9b8a60b4f4402bdc7082b945d77899f1282d2', Expires_in: 86400000 } ] undefined (node:1104) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejec tion id: 1): TypeError: Cannot read property 'toArray' of undefined*
So console.log(doc) is returing the JSON object correctly. but variable remains undefined . I need the access_token in a variable . Where am I going wrong ?