I'm trying out Loopback for an API that will talk to Ember.
Ember requires JSON to be contained in 'keys', e.g. for an account:
{ account:
{ domain: 'domain.com',
subdomain: 'test',
title: 'test.domain.com',
id: 1
} }
I've found some advice on the Google group about how to alter the response so that Ember will receive it, using afterRemote hooks.
E.g. in my models/account.js:
module.exports = function(Account) {
Account.afterRemote('**', function (ctx, account, next) {
if(ctx.result) {
if(Array.isArray(ctx.result)) {
ctx.res.body = { 'accounts': account };
} else {
ctx.res.body = { 'account': account };
}
}
console.log(ctx.res.body);
next();
});
};
I see that the response is as it should be in the console .. however the JSON output at localhost:3000/api/accounts does not show the altered JSON object.
What is the correct way to alter the JSON response / requests in Loopback?
Ideally in a general way so it can be applied to all Models.
ctx.result
is non-null then settingctx.res
. Should you not be usingctx.result.body = ...
? – Acquirement