Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 34 forks source link

`Cannot read property '$toJson' of undefined` when no results returned #28

Closed vjpr closed 6 years ago

vjpr commented 6 years ago
  knex:client acquired connection from pool: __knexUid1 +12s
  knex:query select "user"."id" from "user" +12s
--------------------
query:
  __knexUid:       __knexUid1
  method:          select
  options:
  timeout:         false
  cancelOnTimeout: false
  bindings:
    (empty array)
  __knexQueryUid:  fe9f1ebd-5a2a-42da-8f02-a4929e4d7626
  toNative:        function() {}
  sql:             select "user"."id" from "user"
--------------------
  knex:bindings [] +12s
  knex:client releasing connection to pool: __knexUid1 +1ms
{ result: undefined }
{ TypeError: Cannot read property '$toJson' of undefined

result is null here.

function toJson(result) {
  if (_.isArray(result)) {
    for (let i = 0, l = result.length; i < l; ++i) {
      result[i] = result[i].$toJson();
    }
  } else {
    result = result.$toJson();
  }

  return result;
}
vjpr commented 6 years ago

Tested, works.