I am using Loopback for a node JS app. Loopback auto generates the CRUD api. So, I was trying to alter one get api so also include a table. I can do this by adding include filter in query as
/api/expensecategories?filter[include]=vendors
but I want /api/expensecategories to return with the table. For that I used beforeRemote() method to change the request before.
My code for it is:
expensecategories.beforeRemote('find', function (ctx, inst, next) {
console.log(ctx.req.url);
ctx.req.url = "/?filter[include]=vendors";
console.log(ctx.req.url);
console.log('GET api called');
next();
});
this is changing the request url but not changing the response, it is the same and table is not included. I tried changing everything in req like
ctx.req.query = {filter: { include: 'vendors' }};
but no use. Any idea how can i achieve this. Both tables relation is already defined and I am able to get the desired result by making a custom api like,
expensecategories.expensecategory = function (cb) {
expensecategories.find({
include: {
relation : 'vendors',
}
}, function(err, data) {
cb(null, data);
});
};
expensecategories.remoteMethod (
'expensecategory', {
description: 'get all expense types + vendors',
http: {path: '/yes', verb: 'get'},
returns: {arg: 'expensecategory', type: 'string'}
}
);
So, my table relation and everything is correct. I want the same result for /expensecategories also. Help!!!