DefiJack
DefiJack

Reputation: 163

TypeError: cb is not a function

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

Upvotes: 4

Views: 30528

Answers (1)

Davebra
Davebra

Reputation: 488

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

Upvotes: 3

Related Questions