I have a problem with node.js callback function.
This is my postProcessing.js file.
var express = require('express');
//create router like interface for responce to app.js
var router = express.Router();
//Inject mongoose in context
var mongoose = require('mongoose');
//Create Mongoose Schema object
var Schema = mongoose.Schema;
router.get('/', function(req, res, next) {
var radnikProjId = new Array();
function make(callback) {
mongoose.model('radprojcol').find({'mbr': 50}, function(err, radproji) {
radproji.forEach(function(radproj){
mongoose.model('projekatcol').findOne({'spr': radproj.spr}, function(err, projekat) {
radnikProjId.push({'id':projekat._id});
});
});
});
return callback(radnikProjId);
}
make(function(resp){
console.log(resp)
});
});
module.exports = router;
When I call make function it returns an empty document.
But when I call callback in most inner foreach it returns a good document, with all I need.
What is wrong with this code, can any one help me?
I exprected that my radnikProjId array variable going to be fulfilled with objects in time when I call a callback inside of make function.
Thak you.