回调不是一个函数:我做错了什么

callback is not a function: what am i doing wrong?

本文关键字:函数 什么 错了 一个 回调      更新时间:2023-09-26

我正在对 Redis 进行异步调用并尝试使用回调来通知异步.js查询已完成。 我一直遇到一个错误,指出"回调不是函数"。

我在这里做错了什么?

            "check": function (redisListItem, dataElement, callback) {
                let lookupKey = redisListItem.table + dataElement;
                let virtualField = redisListItem.virtualName;
                client.get(lookupKey, function (err, reply) {
                        if (err) {
                            return callback(err)
                        }
                        else {
                            session.virtual[virtualField] = reply;
                            callback(session);
                        }
                });
            }

对"检查"的调用如下:

    "condition": function(R) {
        var self = this;
        async.series([
            function(R){
/////////THE CALL TO CHECK ////////
                R.check(List.redisTables.List.negEmail, self.customer.email)
            }.bind(this,R),
            function(R) {
                R.when(this.virtual.negEmail === "true")
            }.bind(this,R)
        ])
    }

R.check(List.redisTables.List.negEmail, self.customer.email)只有两个参数,第三个参数,应该是一个函数,丢失了,即它是未定义的

R.check(List.redisTables.List.negEmail, self.customer.email, function(session) {
    // do something when "check()" has completed
})

作为旁注,您应该坚持 Node 约定,并传递错误和数据

client.get(lookupKey, function (err, reply) {
      if (err) {
           return callback(err, null)
      } else {
           session.virtual[virtualField] = reply;
           callback(null, session);
      }
});

这样你就可以实际检查错误

R.check(List.redisTables.List.negEmail, self.customer.email, function(err, session) {
    if (err) throw new Error('fail')
    // do something when "check()" has completed
})