0

I am getting this strange error recently. It was not there earlier and I don't remember changing much.

error: Error (E_UNKNOWN) :: Encountered an unexpected error
TypeError: Cannot convert null to object
    at hasOwnProperty (native)
    at utils.object.hasOwnProperty (/home/mandeep/freelance/hellos/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/utils.js:28:14)
    at /home/mandeep/freelance/hellos/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/where.js:259:11
    at Array.forEach (native)
    at WhereBuilder.complex (/home/mandeep/freelance/hellos/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/where.js:177:36)
    at complexWhere (/home/mandeep/freelance/hellos/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/index.js:244:16)
    at find (/home/mandeep/freelance/hellos/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/index.js:85:23)
    at Cursor.populateBuffers [as $populateBuffers] (/home/mandeep/freelance/hellos/node_modules/sails-postgresql/lib/adapter.js:539:31)
    at Cursor.run (/home/mandeep/freelance/hellos/node_modules/sails-postgresql/node_modules/waterline-cursor/cursor/cursor.js:45:8)
    at runJoins (/home/mandeep/freelance/hellos/node_modules/sails-postgresql/node_modules/waterline-cursor/index.js:51:10)

Details:  TypeError: Cannot convert null to object

The error goes away when I remove the one to many association from user model. Here are the models for reference:

Underlying database is postgres

User.js

module.exports = {

  tableName: "users",

  attributes: {

    name: {
        type: "string",
        required: false
    },

    permission: {
        type: "integer",
      defaultsTo: 2

    },

    primary_phone: {
      model: "phone",
      required: true
    },

    phone: {
        collection: "phone",
        via: "id"
    },

    primary_email: {
      model: "email",
      required: true
    },

    email: {
        collection: "email",
        via: "id"
    }

  }
};

Phone.js

module.exports = {

  attributes: {

    number: {
      type: "string",
      required: true
    },

    owner: {
        model: "user"
    }

  }

};

Email.js

module.exports = {

  attributes: {

    email: {
        type: "email",
        required: true
    },

    owner: {
        model: "user"
    },

    verified: {
        type: "boolean",
        defaultsTo: false
    }   

  }
};
Mandeep Singh
  • 7,674
  • 19
  • 62
  • 104

1 Answers1

0

I don't think you can do via: "id" - the id field doesn't refer back to the User model. You should create a new attribute for both the Email and Phone model and link those back to the User model.

For example,

User.js:

...
phone: {
    collection: "phone",
    via: "nonPrimaryPhoneOwner"
},
email: {
    collection: "email",
    via: "nonPrimaryEmailOwner"
}
...

Email.js:

...
nonPrimaryEmailOwner: {
    model: "user"
}
...

Phone.js:

...
nonPrimaryPhoneOwner: {
    model: "user"
}
...
kk415kk
  • 1,227
  • 1
  • 14
  • 30
  • I have been using this code in the past without any issues. Noticed a weird thing today! It's working fine on my workplace system but not working at my home pc. I will check the sails-postgres adapter versions once I reach home – Mandeep Singh Aug 22 '14 at 07:22