【问题标题】:Async.waterfall : Callback is not a function / module.exportsAsync.waterfall : 回调不是函数/module.exports
【发布时间】:2018-07-16 12:01:14
【问题描述】:

花了几个小时尝试在这里找到多种解决方案后,我决定在此处发布关于该错误的问题(抛出 async.waterfall 中的第一个函数:function1):

C:\wamp64\www\projet\app\node_modules\mysql\lib\protocol\Parser.js:80
    throw err; // Rethrow non-MySQL errors
    ^

TypeError: callback is not a function
at Query._callback (C:\wamp64\www\projet\app\model\mymodel.js:10:3)
at Query.Sequence.end (C:\wamp64\www\projet\app\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
at Query._handleFinalResultPacket (C:\wamp64\www\projet\app\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
at Query.EofPacket (C:\wamp64\www\projet\app\node_modules\mysql\lib\protocol\sequences\Query.js:123:8)
at Protocol._parsePacket (C:\wamp64\www\projet\app\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (C:\wamp64\www\projet\app\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\wamp64\www\projet\app\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\wamp64\www\projet\app\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)

我关注了几篇文章,但我不确定语法或在此控制器和模型中构建代码的方式。我想我错过了一些东西,但我不知道是什么:(

这是 myController.js

const async = require('async');
const MyModel = require('../model/mymodel');

module.exports.respond = function(socket) {
socket.on('connectionUser', function(param1, param2, param3= 'false') {
    try {
        async.waterfall([
            async.constant(param1, param2, param3),
            MyModel.function1,
            MyModel.function2,
        ], function3);
    } catch (err) {
        // ERRORS handler
    }

function funtion3 (err, userInfos, res[0]) {}

在 mymodel.js 中

const db = require('../config);

module.exports.function1 = function(callback){
    let reqSql = 'SELECT ...';
    db.query(reqSql, function(err, res){
        if (err) return callback(err);
        return callback(null, res[0]);
    });
};

module.exports.function2 = function(userInfos, callback){
    let reqSql = 'SELECT ....'
    db.query(reqSql, function(err, res){
        if (err) return callback(err);
        return callback(null, userInfos, res[0]);
    });
};

【问题讨论】:

  • 函数3和函数4在哪里?
  • 要么编辑问题并正确提供详细信息,要么参考caolan.github.io/async/docs.html#waterfall async 文档
  • 感谢您的评论。我已经编辑了我的问题并检查了您添加的链接。

标签: node.js callback async.js


【解决方案1】:

正如错误消息所说。callback 不是函数 1、函数 2 内嵌套函数中的函数,

你需要从嵌套函数中返回回调

试试这个

const db = require('../config);

module.exports.function1 = function(callback){
    let reqSql = 'SELECT ...';
    db.query(reqSql, function(err, res){
        if (err) return callback(err);
        return callback(null, res[0]);
    });
};

module.exports.function2 = function(userInfos, callback){
    let reqSql = 'SELECT ....'
    db.query(reqSql, function(err, res){
        if (err) return callback(err);
        return callback(null, userInfos, res[0]);
    });
};

【讨论】:

  • 感谢您的回答。我尝试了您的解决方案,但错误保持不变
猜你喜欢
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2012-04-11
相关资源
最近更新 更多