Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createQuery on empty table throws an error #147

Closed
roelvan opened this issue Mar 8, 2018 · 0 comments
Closed

createQuery on empty table throws an error #147

roelvan opened this issue Mar 8, 2018 · 0 comments

Comments

@roelvan
Copy link

roelvan commented Mar 8, 2018

Steps to reproduce

Changing the knex query (via createQuery()) in a before hook returns an error when there are 0 rows in my postgres DB:

TypeError: Cannot read property 'total' of undefined
    at countQuery.then.count (/path/to/node_modules/feathers-knex/lib/index.js:184:48)

As soon as there are rows, everything works fine.

My before hook that does a count / join:

const { mapKeys } = require('lodash');

module.exports = (options = {}) => { // eslint-disable-line no-unused-vars
  return async context => {
    // PREVENT: "column reference "[column name]" is ambiguous"
    const sort = mapKeys(context.params.query.$sort, (order, field) => `plannings.${field}`);
    context.params.query.$sort = sort;
    
    const query = context.service.createQuery({ query: context.params.query });
    query
      .count('plannings.id as planninRuleCount')
      .leftJoin('planningRules as plr', 'plr.planningId', 'plannings.id')
      .groupBy('plannings.id');
    
    if (context.id) // Scope the query if it's a single GET call
      query.where('plannings.id', context.id);
    
    context.params.knex = query;
    return context;
  };
};
Vasilcenko added a commit to Vasilcenko/feathers-knex that referenced this issue Mar 27, 2018
@daffl daffl closed this as completed in 4b09e99 Mar 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant