0

我正在使用 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 对我的模型的每个属性调用强制转换函数,即使我的查询中没有使用这样的属性。当然会导致错误。我做了一个补丁:

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

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

有没有人知道如何以另一种方式修复此错误?或者也许我做错了调用查询方法,应该用另一种方式调用它?

4

2 回答 2

2

发生这种情况是因为您的模型属性与表字段不匹配。浏览您的用户模型文件并确保您的所有属性都与您的用户表上的字段匹配。

于 2014-05-04T22:45:17.437 回答
0

据我所知,'user' 是 postgre db 中的保留关键字。这可能是因为这个。

于 2015-05-24T12:13:10.257 回答