-
Notifications
You must be signed in to change notification settings - Fork 29
feathers-hooks is changing the scope of makeWrapper() #17
Comments
Is that also happening with latest master? Feathers |
Correct. I thought that the problem might be here in feathers: https://github.com/feathersjs/feathers/blob/master/lib/mixins/promise.js#L24, because it's not using call or apply to change context? But it's working without It's happening with the latest master(
|
Can you try var userService = fMongo({collection: 'users'}); Looking at the error at https://github.com/feathersjs/feathers-mongodb/blob/master/lib/mongodb.js#L66 it seems as if |
It looks like the problem is happening here: https://github.com/feathersjs/feathers-hooks/blob/master/lib/before.js#L24
|
Oh. I forgot I'm using this branch of feathers-mongodb that I was working on a couple weeks ago: I don't think there is anything that should be changing the outcome, but let me test with the current master. |
I pointed that out because it is complaining about var feathers = require('feathers');
var fHooks = require('feathers-hooks');
var app = feathers()
.use(feathers.static(__dirname + '/public'))
.configure(feathers.rest());
// The users endpoint must match the one setup on config.store.
app.use('api/users', {
number: 10,
find: function(params, callback) {
callback(null, [{
id: this.number;
}])
}
});
var users = app.service('api/users');
// Dummy hook
function hookMe(hook, next){
next();
}
users.before({find:[hookMe]});
// Start the server.
var port = 8080;
app.listen(port, function() {
console.log('Feathers server listening on port ' + port);
}); |
Yeah, good point on doing the simplest test. Even that example fails with the same problem. console.log(this) from within find, using the example you just posted, produces the global scope:
|
It looks like the fix was as simple as putting a |
after() hooks seem to work fine. |
You're right, good catch! Found the issue, will land a patch and make a new release tonight. |
Thanks. And thanks for your help! |
This issue as well as the others are fixed and version 0.5.0 is released. /cc @ekryski |
When using feathers-hooks, the scope of the makeWrapper() function is the global node object. Since that same scope is passed to the
feathers-mongodb
service, it causes an error here ofTypeError: Cannot call method 'find' of undefined
. (this.collection is undefined on the global node object).If I comment out the
users.before()
line in the example code below, the scope ofthis
inside makeWrapper() is the service with its methods.I've tested both the latest commit of
feathers-hooks
and the0.4.0
version from npm.So far this has only been a problem when using feathers-mongodb because of its use of
this
inside the methods.Here is the simple server.js I'm using to test:
The text was updated successfully, but these errors were encountered: