Objection.js: Cannot use 'in' operator to search for 'key' in undefined

Created on 6 Apr 2018  路  10Comments  路  Source: Vincit/objection.js

EDIT BY @koskimas: This has been fixed in 1.1.3

Seems to be a regression in 1.1.2.

When calling JSON.stringify(entities); (which, e. g., happens when you are trying to send an array of Objection entities as a response from Express), an error is thrown.
Error seems to only manifest itself when entity in question has custom idColumn defined (in this particular case it was defined as

    static get idColumn() {
        return 'key';
    }

)

Error stacktrace:

TypeError: Cannot use 'in' operator to search for 'key' in undefined
at assignJsonValue (C:sourceslms-apimodelsnode_modulesobjectionlibmodelmodelToJson.js:79:33)
at toExternalJsonImpl (C:sourceslms-apimodelsnode_modulesobjectionlibmodelmodelToJson.js:51:5)
at toJson (C:sourceslms-apimodelsnode_modulesobjectionlibmodelmodelToJson.js:20:14)
at Brand.$toJson (C:sourceslms-apimodelsnode_modulesobjectionlibmodelModel.js:204:12)
at Brand.toJSON (C:sourceslms-apimodelsnode_modulesobjectionlibmodelModel.js:208:17)
at JSON.stringify ()
at eval (eval at stringify (C:sourceslms-apiapinode_modulesexpresslibresponse.js:1117:14), :1:6)
at stringify (C:sourceslms-apiapinode_modulesexpresslibresponse.js:1117:14)
at ServerResponse.json (C:sourceslms-apiapinode_modulesexpresslibresponse.js:260:14)
at ServerResponse.send (C:sourceslms-apiapinode_modulesexpresslibresponse.js:158:21)
at router.get (C:sourceslms-apiapicontrollersbrands.controller.js:44:7)
at

Surprisingly, this works fine when single entity is being stringified.

bug

Most helpful comment

Fixed. A new version will be out in a couple of minutes. Sorry about this 馃槵

All 10 comments

I'll need a repro for this. I was not able to reproduce this.

This works just fine:

const { Model } = require('objection');

class Person extends Model {
  static get idColumn() {
    return 'key';
  }
}

const p1 = Person.fromJson({});
const p2 = Person.fromJson({});

JSON.stringify([p1, p2]);

@koskimas Will create a failing test ASAP

Silly me, this throws:

const { Model } = require('objetion');

class Person extends Model {
  static get idColumn() {
    return 'key';
  }
}

const p1 = Person.fromJson({ key: 1 });
const p2 = Person.fromJson({ key: 2 });

JSON.stringify([p1, p2]);

@igor-savin-ht No need for repro anymore.

Fixed. A new version will be out in a couple of minutes. Sorry about this 馃槵

1.1.3 is now released. I'll reopen this with another name so that other people can find it.

@koskimas PR with reproduction test is up.

ah, sorry, missed your update. Thank you for the quick fix!

@koskimas Shouldn't this be closed :)? Published fix worked, btw.

@kibertoad Other people may have hit the same bug and they'll come looking for an answer. Let's keep this open for a couple of days.

Makes sense.

Was this page helpful?
0 / 5 - 0 ratings