I have a situation where i am badly stuck. Tried a lot but no worth.
I have a nodejs application, where i am reading data from mongodb.
I have a main method, from where I am calling a fetchMongo()
method and all other functions. I need the fetchMongo
function to return the result to the Main. The main method has an object obj
that will get passed to all the functions.
The code below I tried: Its not working.
console.log
inside fetchMongo()
works fine, but fetchMongo()
is not returning the value to Main.
Please help.
function fetchMongo(obj)
{
var defer = Q.defer();
serviceHealthConfig.findOne({}, '', { sort: {'_id' : -1} }, function(err, nodes){
var struct={}
if(err)
console.log(err);
//console.log(nodes);
obj = nodes;
return obj;
});
defer.resolve(obj);
return defer.promise;
}
function main(){
var obj = { };
var nodesObject = fetchMongo(obj)
.then(methOne(obj))
.then(methTwo(obj))
.then(methThree(obj))
}
Your defer.resolve should be called inside the find one callback. Like below:
function fetchMongo(obj)
{
var defer = Q.defer();
serviceHealthConfig.findOne({}, '', { sort: {'_id' : -1} }, function(err, nodes){
var struct={}
if(err)
console.log(err);
//console.log(nodes);
obj = nodes;
defer.resolve(obj);
});
return defer.promise;
}
Moreover findOne needs a parameter as well; what to find.
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.