TypeError:cb不是函数

时间:2019-07-08 01:18:07

标签: javascript node.js mongodb

我被困住了。我不断得到相同的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

1 个答案:

答案 0 :(得分:0)

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

我从您的代码中看到,由于数据库操作,您只需要传递true / false,因此只需从函数中删除cb参数,然后仅返回true / false:

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;
        }
    });
}