Reputation: 520
My app needs to return a JSON response after some DB queries.
Pseudocode (using async
):
async.forEachOf(sails.config.site.regions, function (regionData, slug) {
obj['regions'][slug] = {
'slug': slug,
'name': regionData.name,
};
}, function (err) {
if (err) {
res.json({
'error': true,
'message': 'Error when retrieving data from the DB'
});
}
else {
res.json(obj);
return;
}
});
Sails hangs since no response seems to be returned from the service.
I tried converting this to a Promise (see this answer) but it doesn't work either.
How can I do this?
Upvotes: 1
Views: 523
Reputation: 5979
Have you tried
async.forEachOf(sails.config.site.regions, function (regionData, slug) {
obj['regions'][slug] = {
'slug': slug,
'name': regionData.name,
};
}, function (err) {
if (err) {
return res.json({
'error': true,
'message': 'Error when retrieving data from the DB'
});
}
else {
return res.json(obj);
}
});
Upvotes: 1