I have a problem I can't find an answer to in Loopback's docs.
Say I have a model Company
and a modelEmployee
. There is an 1Xn relation between the Company
and its Employees
. When /api/Employees
is called, server returns all the employees.
I only want to return the list of employees who are in the same pany with the user requesting the list.
For this, I created a remote hook
Employee.beforeRemote('find', function(context, modelInstance, next) {
var reject = function() {
process.nextTick(function() {
next(null, false);
});
};
// do not allow anonymous users
var userId = context.req.accessToken.userId;
if (!userId) {
return reject();
}
//I get the details of the user who sent the request
//to learn which pany does he belong to
Employee.findById(userId, function(err, user) {
if(!context.req.query.filter) context.req.query.filter={};
context.req.query.filter.where = {brandId:userpanyId};
console.log(context.req.query);
next();
});
});
I thought this should work every time, but appearantly it only works when find already has some query filters like include - although the console.log prints a correct context.req.query object.
What am I missing? Any help would be greatly appreciated!
I have a problem I can't find an answer to in Loopback's docs.
Say I have a model Company
and a modelEmployee
. There is an 1Xn relation between the Company
and its Employees
. When /api/Employees
is called, server returns all the employees.
I only want to return the list of employees who are in the same pany with the user requesting the list.
For this, I created a remote hook
Employee.beforeRemote('find', function(context, modelInstance, next) {
var reject = function() {
process.nextTick(function() {
next(null, false);
});
};
// do not allow anonymous users
var userId = context.req.accessToken.userId;
if (!userId) {
return reject();
}
//I get the details of the user who sent the request
//to learn which pany does he belong to
Employee.findById(userId, function(err, user) {
if(!context.req.query.filter) context.req.query.filter={};
context.req.query.filter.where = {brandId:user.panyId};
console.log(context.req.query);
next();
});
});
I thought this should work every time, but appearantly it only works when find already has some query filters like include - although the console.log prints a correct context.req.query object.
What am I missing? Any help would be greatly appreciated!
Share Improve this question edited Mar 31, 2015 at 16:28 Marc 3,7098 gold badges36 silver badges49 bronze badges asked Mar 31, 2015 at 16:04 Mihaly KRMihaly KR 2,3732 gold badges21 silver badges20 bronze badges 2-
Are you saying that the
beforeRemote()
hook function is not being called when there is no filter passed tofind()
, or that your code specifically is not working when no filter is provided? – Jordan Kasper Commented Mar 31, 2015 at 17:30 - The problem was that context.req.query.filter is ignored when it wasn't set from the client request. Alex's suggestion worked though, thank you all for your help. – Mihaly KR Commented Apr 1, 2015 at 7:04
1 Answer
Reset to default 11context.args.filter
seems to work for this purpose.
As a side note, instead of replacing where
, you might want to merge it with something provided by client. For implementation idea you can refer to: https://github./strongloop/loopback-datasource-juggler/blob/master/lib/utils.js#L56-L122