最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: cb is not a function - Stack Overflow

programmeradmin0浏览0评论

I'm stuck. I keep getting the same cb is not a function error. Here's my error:

TypeError: cb is not a function

I just started learning javascript a few days ago, so I'm very new. I'm simply watching youtube videos that do what I need done for my application and I write what they write. So far it's been going well, had a few problems that I managed to fix on my own. But this one I can't figure out. So a little help would be very much appreciated.

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

});

});

I keep getting this error:

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

I'm stuck. I keep getting the same cb is not a function error. Here's my error:

TypeError: cb is not a function

I just started learning javascript a few days ago, so I'm very new. I'm simply watching youtube videos that do what I need done for my application and I write what they write. So far it's been going well, had a few problems that I managed to fix on my own. But this one I can't figure out. So a little help would be very much appreciated.

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

});

});

I keep getting this error:

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
Share Improve this question asked Jul 8, 2019 at 1:18 DefiJackDefiJack 1631 gold badge2 silver badges7 bronze badges 2
  • 1 You aren't passing any cbs to any of the three functions that require a callback as a second parameter – CertainPerformance Commented Jul 8, 2019 at 1:19
  • Your code doesnot show how you are passing the value in cb. as the error said, the value you oassed in cb is not a function. You need to pass by refernce – Tushar Gupta Commented Jul 8, 2019 at 1:20
Add a ment  | 

1 Answer 1

Reset to default 3

Wele to Stackoverflow, the cb is usually referred as callback function to pass to another function, I think in your code you don't need this. Probably you referenced the code from the documentation of Socket.io or MongoDB where they often use to pass a callback function as result.

I see from your code that you just need to pass true/false as result of db operation, so just remove the cb parameter from your functions and return just 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;
        }
    });
}
发布评论

评论列表(0)

  1. 暂无评论