0

I'm using Waterline (Postgresql database) method Model.query(). For example: (the sql query is actually much more complicated :) )

Model.query('select * from user', function(err, result) {

});

and there is such error:

TypeError: Cannot read property 'type' of undefined
  at /node_modules/sails-postgresql/lib/query.js:544:33

This is the code in the sails-postgresql module, where the error occurs:

Object.keys(values).forEach(function(key) {

    // Lookup schema type
    var type = self._schema[key].type;
    if(!type) return;

    // Attempt to parse Array
    if(type === 'array') {
      try {
        _values[key] = JSON.parse(values[key]);
      } catch(e) {
        return;
      }
    }

  });

So, I've printed the values and keys like that

console.log('self._schema[' + key + '] = ' + self._schema[key]);

and found out that Waterline calls cast function to every attruibute of my Model, even there is no such attribute used in my query. Ofcourse it causes error. I've made a patch:

if(!self._schema[key]) return;

and now it works fine. But I don't think that this is the right solution.

Is there anybody who knows how to fix this bug another way? Or maybe I'm doing wrong calling query method and it should be called another way?

Stewie
  • 11
  • 4

2 Answers2

2

This happens because your model attributes are mismatched with your table fields. Go through your User model file and make sure all your attributes are matched with the fields on your User table.

Ken Goo
  • 101
  • 1
  • 4
  • 1
    Or you can add `migrate: 'drop'` to your development models to auto create the table. See: [Sails Docs - Model Settings](http://sailsjs.org/#/documentation/concepts/ORM/model-settings.html) – eldermao Dec 01 '14 at 22:25
0

As i know 'user' is reserved keyword in postgre db. That might happen because of this.