feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
14.97k stars 742 forks source link

Wrong value for context.method in create hook #3428

Closed lukvermeulen closed 4 months ago

lukvermeulen commented 4 months ago

Context

I noticed the query resolver running on create methods - causing my app to prevent the creation of an item since I was limiting the query by a property that does not exist for the user without creating sad item (a user needs to create a tenant, that is then assigned to him. He can only view tenant information of his own tenant - thats what the query resolver is for.)

To keep my getTenantId resolver generic, I added a configuration option to skip the check on certain methods (e.g. create), while keeping the query resolver itself intact on all methods.

Error

Thats when I noticed, that context.method returned findin all cases - even on create calls.

Steps to reproduce

I discovered logging context.method always prints "find", even in hooks.

Adding this example hook

async (context) => {
  console.log(context.method);
},

to the before > create hook also returns findwhen creating an object.

before: {
  all: [schemaHooks.validateQuery(channelQueryValidator), schemaHooks.resolveQuery(channelQueryResolver)],
  find: [],
  get: [],
  create: [
    async (context) => {
      console.log(context.method);
    },
    schemaHooks.validateData(channelDataValidator),
    schemaHooks.resolveData(channelDataResolver),
  ],
  patch: [schemaHooks.validateData(channelPatchValidator), schemaHooks.resolveData(channelPatchResolver)],
  remove: [],
},

Expected behavior

I'm not sure what is causing this, as this happens on all services for me. I expect the method to return the name of the method, e.g. create, get, ...

Actual behavior

context.methodholds the value findin all cases, ignore the actual method type that was called.

System configuration

Tell us about the applicable parts of your setup.

Module versions: @featherjs .. tested with 5.0.9, 5.0.21

NodeJS version: 18.17

Operating System: mac os

daffl commented 4 months ago

I am not able to reproduce this and would be surprised if this was a general issue since many applications and plugins are using similar checks. Please provide a full repository that illustrates your problem.

lukvermeulen commented 4 months ago

Thanks alot for the heads up. I dug into it again and it looks like some noise made me miss a stupid typo. Sorry for the noise!

daffl commented 4 months ago

That's ok, if it was something that was easy to break it would've still made sense to look into.