FeathersJs: context.params.user returning undefined in before hook

669 Views Asked by At

I'm getting undefined when I try to assign user._id to an entity in a before hook.

The hook

module.exports = (options = {}) => { 
return async context => { 

const user = context.params.user;

context.data = {
...
      userId: user._id,
...
    };
return context; 
}; 
};

This is my hooks register

  before: {
...
    create: [processProperty(), authenticate('jwt')],
...
  }
}
1

There are 1 best solutions below

0
Aosu Terver On

I was able to fix the issue by bringing the authenticate hook before my processProperty hook as shown below. I think this is because each hook in the array is processed from left to right, so in this case, the processProperty hook needs access to the authenticated user, so it needs to come after the authenticate hook has been processed.

  before: {
...
    create: [authenticate('jwt'), processProperty()],
...
  }
}