【问题标题】:TypeError: cb is not a function类型错误:cb 不是函数
【发布时间】:2019-07-08 01:18:07
【问题描述】:

我被困住了。我不断得到相同的 cb 不是函数错误。这是我的错误:

TypeError: cb 不是函数

几天前我刚开始学习javascript,所以我很新。我只是在观看 youtube 视频,这些视频完成了我的应用程序需要做的事情,然后我写了他们写的东西。到目前为止一切顺利,有一些我自己设法解决的问题。但是这个我想不通。非常感谢您的帮助。

var isValidPassword = function(data,cb) {
    db.users.find({username:data.username,password:data.password},function(err,res) {
        if (res.length > 0) {
           return cb(true);
        } else {
           return cb(false);
        }
    });
}

var isUsernameTaken = function(data,cb) {
    db.users.find({username:data.username},function(err,res) {
        if (res.length > 0) {
           return cb(true);
        } else {
           return cb(false);
        }
    });
}

var addUser = function(data,cb) {
    db.users.insert({username:data.username,password:data.password},function(err) {
       return cb();
    });
}

io.on('connection', (sock) => {
    sock.id = Math.random();
    SOCKET_LIST[sock.id] = sock;
    console.log('someone connected');

    sock.on('signIn', function(data) {
        if (isValidPassword(data)) {
            sock.emit('signInResponse', {success:true});
        } else {
            sock.emit('signInResponse', {success:false});
        }

    });

sock.on('signUp', function(data) {
    if (isUsernameTaken(data)) {
        sock.emit('signUpResponse', {success:false});
    } else {
        addUser(data);
        sock.emit('signUpResponse', {success:true});
    }

});

});

我不断收到此错误:

TypeError: cb is not a function
    at C:\Users\user\Desktop\Mekkie\mekkie\testlogin.js:32:19
    at C:\Users\user\Desktop\Mekkie\mekkie\node_modules\mongojs\lib\cursor.js:73:24
    at AsyncResource.runInAsyncScope (async_hooks.js:188:21)
    at runInAsyncScope (C:\Users\user\Desktop\Mekkie\mekkie\node_modules\mongojs\lib\cursor.js:195:16)
    at C:\Users\user\Desktop\Mekkie\mekkie\node_modules\mongojs\lib\cursor.js:205:5
    at handleCallback (C:\Users\user\Desktop\Mekkie\mekkie\node_modules\mongojs\node_modules\mongodb\lib\utils.js:120:56)
    at C:\Users\user\Desktop\Mekkie\mekkie\node_modules\mongojs\node_modules\mongodb\lib\cursor.js:683:5
    at handleCallback (C:\Users\user\Desktop\Mekkie\mekkie\node_modules\mongojs\node_modules\mongodb-core\lib\cursor.js:171:5)
    at setCursorNotified (C:\Users\user\Desktop\Mekkie\mekkie\node_modules\mongojs\node_modules\mongodb-core\lib\cursor.js:515:3)
    at C:\Users\user\Desktop\Mekkie\mekkie\node_modules\mongojs\node_modules\mongodb-core\lib\cursor.js:599:16

【问题讨论】:

  • 您没有将任何cbs 传递给需要回调作为第二个参数的三个函数中的任何一个
  • 您的代码没有显示您如何传递 cb 中的值。正如错误所说,您在 cb 中 oassed 的值不是函数。你需要通过引用传递

标签: javascript node.js mongodb


【解决方案1】:

欢迎来到 Stackoverflow,cb 通常被称为回调函数以传递给另一个函数,我认为在你的代码中你不需要这个。可能您引用了 Socket.io 或 MongoDB 文档中的代码,它们经常用于传递回调函数作为结果。

我从您的代码中看到,您只需要传递真/假作为 db 操作的结果,因此只需从您的函数中删除 cb 参数并仅返回真/假:

var isValidPassword = function(data) {
    db.users.find({username:data.username,password:data.password},function(err,res) {
        if (res.length > 0) {
           return true;
        } else {
           return false;
        }
    });
}

var isUsernameTaken = function(data) {
    db.users.find({username:data.username},function(err,res) {
        if (res.length > 0) {
           return true;
        } else {
           return false;
        }
    });
}

var addUser = function(data) {
    db.users.insert({username:data.username,password:data.password},function(err) {
       if (err) {
           return false;
        } else {
           return true;
        }
    });
}

【讨论】:

    猜你喜欢
    • 2019-11-09
    • 2023-01-04
    • 2016-02-26
    • 2013-04-01
    • 2016-03-06
    • 2018-04-28
    • 2019-02-14
    • 2021-10-02
    相关资源
    最近更新 更多