【问题标题】:Sailsjs Waterline's Query method calls cast function to undefined attributesSailsjs Waterline 的 Query 方法调用 cast 函数到未定义的属性
【发布时间】:2014-05-28 13:59:35
【问题描述】:

我正在使用 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;

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

有谁知道如何以另一种方式修复此错误? 或者我调用查询方法的方法有误,应该以另一种方式调用?

【问题讨论】:

    标签: node.js postgresql sails.js waterline


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-05-29
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 2018-08-14
      • 1970-01-01
      • 2014-04-23
      相关资源
      最近更新 更多