You cannot mix synchronous return with asynchronous functions such as findById , as this contradicts the basic nature of asynchronous operations: "exit now, finish later".
You will need to configure getMetadata to accept the callback function, which can be called when findById completed later:
exports.getMetadata = function (id, callback) {
And instead of returning metadatas only when err does not exist:
if (!err) { return metadatas; } else {
You want to call callback anyway:
callback(err, err ? null : metadatas);
In the context, this may be:
FolderInfo.find({}, null, null, function (err, metadatas) { if (err) { console.log(err); } else { console.log(metadatas); } callback(err, err ? null : metadatas); });
Or maybe just:
FolderInfo.find({}, null, null, callback);
Then pass getMetadata a callback function that processes the response when called:
app.get('/metadata', function(req, res) { console.log("GET: metadata"); database.getMetadata(null, function (err, data) { res.send(data); }); });