数据源管理器代码中的环回3“ TypeError:inst.errors.add不是函数”

时间:2019-02-07 02:10:02

标签: node.js loopback

我正在尝试在模型之间执行自定义验证,基本上是在允许创建新的子模型实例(声明)之前先验证父模型实例(辩论)是否存在。

例如:

const _  = require('lodash');

model.exports = function(Statement) {
    Statement.validateAsync('debateId', function(err, done) {
        const that = this;
        const Debate = _.get(Statement, 'app.models.Debate');
        Debate.exists(this.debateId, function(error, exists) {
            if (error) err(error);
            if (!exists) err(new Statement.ValidationError(that)); //<--line 71
            done();
        });
    }, { 'message': 'Debate not found.' });
}

但是,在测试中,当我故意通过不良的辩论ID时会收到以下消息

TypeError: inst.errors.add is not a function
    at onerror (/home/wolferz/workspaces/resolution/node_modules/loopback-datasource-juggler/lib/validations.js:658:37)
    at Debate.exists (/home/wolferz/workspaces/resolution/server/models/statement.js:71:26)
    at /home/wolferz/workspaces/resolution/node_modules/loopback-datasource-juggler/lib/dao.js:1275:7
    at /home/wolferz/workspaces/resolution/node_modules/loopback-connector/lib/sql.js:1529:7
    at /home/wolferz/workspaces/resolution/node_modules/loopback-datasource-juggler/lib/observer.js:250:22
    at doNotify (/home/wolferz/workspaces/resolution/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)
    at MySQL.ObserverMixin._notifyBaseObservers (/home/wolferz/workspaces/resolution/node_modules/loopback-datasource-juggler/lib/observer.js:178:5)
    at MySQL.ObserverMixin.notifyObserversOf (/home/wolferz/workspaces/resolution/node_modules/loopback-datasource-juggler/lib/observer.js:153:8)
    at cbForWork (/home/wolferz/workspaces/resolution/node_modules/loopback-datasource-juggler/lib/observer.js:240:14)
    at /home/wolferz/workspaces/resolution/node_modules/loopback-connector/lib/sql.js:643:9
    at handleResponse (/home/wolferz/workspaces/resolution/node_modules/loopback-connector-mysql/lib/mysql.js:193:17)
    at Query.<anonymous> (/home/wolferz/workspaces/resolution/node_modules/loopback-connector-mysql/lib/mysql.js:204:7)
    at Query.<anonymous> (/home/wolferz/workspaces/resolution/node_modules/mysql/lib/Connection.js:502:10)
    at Query._callback (/home/wolferz/workspaces/resolution/node_modules/mysql/lib/Connection.js:468:16)
    at Query.Sequence.end (/home/wolferz/workspaces/resolution/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
    at Query._handleFinalResultPacket (/home/wolferz/workspaces/resolution/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)

这将立即使节点服务器崩溃。它应该做的是返回一条错误消息“找不到辩论”。通过API发送给用户。

我在loopback-datasource-juggler/lib/validation.js中的指定行上设置了一个断点,并检查了该点的应用程序状态。供参考的代码是:

if (kind !== false) inst.errors.add(attr, message, code);

kind是我传递给err()回调的验证错误,而inst是正在操作的Statement模型实例(与我上面的代码中的this相同) )。 (很明显,不将任何内容传递给err()会产生与undefined !== false相同的结果)。

问题在于,在执行的这一点上,inst.errors是一个布尔值(在这种情况下为布尔值false)。而且,正如我确定每个人都知道的那样,布尔值没有add()方法。 似乎没有与我的代码有任何关系。 那有什么用呢?这是回送中的错误吗?我是否错过了某些东西(对于Loopback文档多么参差不齐,这真的不会感到惊讶吗?)

节点8.15.0

环回3.24.2

0 个答案:

没有答案