I'm having trouble getting mongodb data into nodejs array as follow:
Test db:
{
"supportTicket" : "viT8B4KsRI7cJF2P2TS7Pd0mfqaI5rtwf",
"msgId" : 1379304604708.0,
"username" : "Guest-OSsL2R",
"message" : "hello",
"_id" : ObjectId("5236849c3651b78416000001")
}
Nodejs:
function _getMsg(st, callback) {
db.test.find({ supportTicket: st }).toArray(function (err, docs) {
callback(docs);
});
}
var nodeArr = _getMsg('viT8B4KsRI7cJF2P2TS7Pd0mfqaI5rtwf', function (res) {
console.log(res); // --> res contain data and printed ok
return res;
});
console.log('return data: ' + nodeArr ) // --> However, nodeArr is undefined.
And here is the result:
return data: undefined
return data: undefined
[ { supportTicket: 'viT8B4KsRI7cJF2P2TS7Pd0mfqaI5rtwf',
msgId: 1379304604708,
username: 'Guest-OSsL2R',
message: 'dhfksjdhfkj',
_id: 5236849c3651b78416000001 } ]
[ { supportTicket: 'viT8B4KsRI7cJF2P2TS7Pd0mfqaI5rtwf',
msgId: 1379304604708,
username: 'Guest-OSsL2R',
message: 'dhfksjdhfkj',
_id: 5236849c3651b78416000001 } ]
The question is: How can I get data from test db and assign the data to nodeArr?