Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter() does not seem to work when passed through middleware #77

Open
swhite2 opened this issue Nov 25, 2019 · 3 comments
Open

filter() does not seem to work when passed through middleware #77

swhite2 opened this issue Nov 25, 2019 · 3 comments

Comments

@swhite2
Copy link

swhite2 commented Nov 25, 2019

Hi there, loving your work! I am however running into an issue with the permission.filter() function. It seems ES6's native implementation of filter() is called when a Permission object is passed through the app with res.locals.permission. Which means that if I try to do this: res.json(res.locals.permission.filter(req.user)); it returns an empty object.

My models are defined as .readOwn('user', [ 'name', 'email', 'phone' ]), and according to the documentation I should expect just the name, email and phone as a response.

@AndreasWJ
Copy link

Found any fix or workaround?

Have the same problem, however I'm just passing the filter function forward by binding the permission object.
req.filterFn = permission.filter.bind(permission);

@TheLartians
Copy link

I've run into a similar issue using Mongoose Documents, where permission.filter(user) returned empty objects. My current workaround is converting the document to an object first via permission.filter(user.toObject()).

@noclat
Copy link

noclat commented Jan 14, 2021

Yep, that's because res.locals serializes data, it's not meant to hold instances. You can pass them across middlewares using the req object, like req.permissions = [/* ... */] and there you'd be able to access all its methods.
Though, AccessControl exposes the filter method on its instance as well (ac.filter()), so maybe there's no need to access it from the permission.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants