feathers-hooks-common icon indicating copy to clipboard operation
feathers-hooks-common copied to clipboard

fastJoin Error

Open Ryan8765 opened this issue 4 years ago • 3 comments
trafficstars

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)"

Ryan8765 avatar Nov 03 '21 02:11 Ryan8765

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

skinofstars avatar Nov 16 '21 13:11 skinofstars

+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.

strarsis avatar Apr 02 '22 21:04 strarsis

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.

skinofstars avatar Apr 05 '22 10:04 skinofstars