Could someone explain how this is happening? An array from Express is being destroyed before it is returned. In the example below, I am successfully populating the resData.user.apps
array and can see this via the server's console log. However, when the resData
object is returned the array is empty:
// Build the response
var resData = {
success: true,
user: {
email: newUserItem.email,
apps: []
}
};
// Add any default applications
Application.find({ isDefault: true, isDeleted: false }, function(err, defaultApps){
if (err){
console.log(err);
return res.status(500).send({ message: 'Failure loading default applications.' });
} else {
if (defaultApps.length < 1){
console.log('No default apps');
} else {
defaultApps.forEach(function(defaultApp){
var app = {
name: defaultApp.formalName,
url: defaultApp.url,
def: true
};
console.log(app);
resData.user.apps.push(app);
console.log('Sanity: ' + JSON.stringify(resData));
});
}
}
});
return res.send(resData);