I am trying to combine the result of two functions calls and return a json object as the output to my api call.
getUserList function retries the list of registered users and averageUserAge function returns the average age of people belongs to a particular age group
In merge function i want to combine the output of both and return a json object.
Can some one explain how can i achieve this?
Please note that i am not looking for aggregate function as it doesn't yield the expected output in my business case.Below is the sample code written to demonstrate what i am looking for as my business case too complex to explain.
function getUserList(req,res){
userModel.findOne({ageGroup : 'some group'},{name : 1,email : 1, credits : 1},{}, function(err, result) {
if( err || !result) {
console.log("No user found");
} else {
return result
};
});
}
function averageUserAge(req,res) {
ageModel.findOne({ageGroup : 'some group'},{email : 1, credits : 1},{},
function(err, result) {
if( err || !docs) {
console.log("No users with a particular age group found");
} else {
return result
};
});
}
//Merge the output of two function calls
function merge(req,res){
var result1=getUserList(req,res)
var result2=averageUserAge(req,res)
var mergedResult=//How to merge result1 & result2
return res.json(mergedResult)
}