feathersjs-ecosystem / feathers-hooks-common

Useful hooks for use with FeathersJS services.
https://hooks-common.feathersjs.com
MIT License
193 stars 89 forks source link

fastJoin Error #645

Open Ryan8765 opened 3 years ago

Ryan8765 commented 3 years ago

Version: 5.0.6 with feathers version 4.5.11

I'm working with fastJoin and for some reason unable to get it to work. Code:

const postResolvers = {
  joins: {
    conf: (...args) => async (connection, context) => {
      try {
        var conf = await context.app.service('con-configs').find({
          query: {
            type: connection.type
          }
        });
      } catch (error) {
        return Promise.reject(new GeneralError(`Error`));
      }
      connection.conf= conf.data[0];
    }
  }
};

I'm using this in a before hook:

before: {

    find: [
      fastJoin(postResolvers)
    ]
  }

Error message:

"TypeError: Cannot read property 'data' of undefined\n at /app/node_modules/feathers-hooks-common/lib/services/fast-join.js:13:48\n at /app/node_modules/feathers-hooks-common/node_modules/@feathersjs/commons/lib/hooks.js:116:46\n at processTicksAndRejections (node:internal/process/task_queues:94:5)"

skinofstars commented 3 years ago

Not sure if you resolved this, but you probably want to change your hook from a before to an after.

strarsis commented 2 years ago

+1, I have the same issue:

@feathersjs/express/rest Error in handler: `Cannot read properties of undefined (reading 'data')` +5ms
[...]
@feathersjs/errors GeneralError(500): Cannot read properties of undefined (reading 'data') +0ms

I can use the hook in after instead of before – but I thought the fastJoin hook also handles upserts (what depopulate would handle). This isn't the case when the hook is only used in after where results can be modified, but not what is processed in upserts.

skinofstars commented 2 years ago

As far as I'm aware (very happy if someone can show me I'm wrong), fastJoin only works in after hooks. It takes the data the service has loaded and uses that data to find the related items.

I personally hit issues with this when I'm looking to filter on joined data. In these cases I modify the knex query (I'm almost all in PostgresSQL) in a before hook. Other possible solutions might be: custom service where you do all querying/updating in service calls; using view tables; pre-loading data in before hooks.