Sailsjs Waterline的Query方法将强制转换函数调用为未定义的属性

时间:2014-04-14 14:37:12

标签: node.js postgresql sails.js waterline

我正在使用Waterline(Postgresql数据库)方法Model.query()。 例如:( sql查询实际上要复杂得多:))

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

});

并且存在这样的错误:

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

这是sails-postgresql模块中的代码,其中发生错误:

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;
      }
    }

  });

所以,我打印了像这样的值和键

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

并发现Waterline调用强制转换函数到我的Model的每个attruibute,即使我的查询中没有使用这样的属性。当然它会导致错误。 我做了一个补丁:

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

现在它工作正常。但我不认为这是正确的解决方案。

有没有人知道如何以另一种方式解决这个问题? 或者也许我在调用查询方法时做错了,应该用另一种方式调用它?

2 个答案:

答案 0 :(得分:2)

这是因为您的模型属性与表字段不匹配。浏览用户模型文件,确保所有属性都与User表中的字段匹配。

答案 1 :(得分:0)

我知道'user'是postgre db中的保留关键字。因此可能会发生这种情况。