I am using nodejs code:
var ret = firstfunction(); //this calls database and gets some return value
var output = secondfunction(ret); // in this function we used ret as parameter
Before completion firstfunction()and before receiving the value "ret", the second function is executed with the parameter "undefined", since "ret" is not yet available, which leads to an error.
How to perform the second function, only after the completion of the first function.
var uname = "sachin";
var noqq=UserModel.find({uname:uname},function(err,user){
if(!err){
myid=user[0]._id;
return myid;
}else {
return null;
}
});
The function below should be executed only after the above is completed, that is, after receiving "myid".
var ret=CollegeModel.findById(myid, function(err,colleges){
if(!err)
{
res.send(questions);
}
else {
res.send(err);
}
});
Please show me the answer implementing my code. Thanks
source
share