Dmitry Mogilevich Dmitry Mogilevich - 1 year ago 58
Node.js Question

How to return value in collection.find({})

var shortLinks = [];

Link.find({}, function (err, links) {

if (err) {

} else { => {

console.log(shortLinks);//shortLinks has values, all okey

console.log(shortLinks); //shortLinks is empty

i need to use shortLinks after Link.find({}) but array is empty.
Need to return shortLinks.


Callbacks. The function(err, links) is called asynchronously, so shortLinks isn't populated until that function is called. Your bottom console.log is being called first, due to how callbacks work.