简体   繁体   中英

Error callback is not a function in node.js

I'm quite new to JS and I don't really understand callbacks and I have looked for a fix for this error but I can't seem to find it. When I execute the code I get this error : TypeError: callback is not a function -> callback(false);

function doesLobbyExist(a, callback) {
  lobbyExists(a, function(random_data) {
    callback(random_data);
  });
}
function lobbyExists(a, callback) {
  if(lobbies.length > 0){
        lobbies.forEach(function(l) {
            if(l.lobbyName == a){
                console.log(a+" exists!");
                callback(true);
            }
        });
    }else{
        callback(false);
    }
}

And I call it like this:

doesLobbyExist(a.ln, function(result) {
            console.log(result);
        });

PS the code goes through console.log(a+" exists!");

It seems to me you're overcomplicating things, callbacks are simple to understand when you get the basic concept. I'll just try to leave a little primer on callback functions here and perhaps you can figure out where you went wrong in your own implementation. Consider the following code:

 function lobbyExists(a, callback) { if (a === 1) { callback(true); } else { callback(false); } } lobbyExists(1, function(response) { console.log(response); }); lobbyExists(0, function(response) { console.log(response); }); 

In this case, you are passing the entire function(response) { console.log(response); } function(response) { console.log(response); } function as a reference in the variable callback in lobbyExists . This means when executing lobbyExists , the variable callback now refers to that function. When you say callback(true) you are therefore calling function(response) { console.log(response); } function(response) { console.log(response); } where response is true .

I executed the code snippets and it worked fine for me. Run the code below.

 var lobbies = [{lobbyName:"a"}] function doesLobbyExist(a, callback) { lobbyExists(a, function(random_data) { callback(random_data); }); } function lobbyExists(a, callback) { if(lobbies.length > 0){ var bool = false lobbies.forEach(function(l) { if(l.lobbyName == a){ bool = true; } }); callback(bool); return false; } callback(false); } doesLobbyExist("a", function(result) { console.log(result); }); 

However I changed lobbyExists function a little since callback would not call if there was no match found.

If you couldn't make it work try changing the variable name of callbacks in each function Ex : Instead of "callback" use "cb"

More about callbacks

Similar question on stack

I'm unable to reproduce what you're seeing. Running your code under a couple of different conditions, I got the following results:

> var lobbies = [{ lobbyName: 'foo' }];
> doesLobbyExist('foo', console.log)
foo exists!
true
> doesLobbyExist('bar', console.log)
...
> var lobbies = [{ lobbyName: 'foo' }, { lobbyName: 'bar' }, { lobbyName: 'foo' }];
> doesLobbyExist('bar', console.log)
bar exists!
true
> doesLobbyExist('foo', console.log)
foo exists!
true
foo exists!
true
...
> var lobbies = [];
> doesLobbyExist('foo', console.log)
false

but there a couple of problems in your code:

  • lobbyExists only gives a false response if there are no lobbies to check, it's possible for it to call the callback multiple times if there are multiple lobbies with the same name, and it doesn't return anything if a lobby isn't found.
  • there's no easy way to break out of forEach . since you only want to call your callback function once, you'll want to either return or break once a lobby has been found. switching over to a for i loop allows us to do either one.
  • comparison using == is what's known as loose or abstract equality, which may cause errors down the road - you can read up on the difference between loose (==) and strict (===) equality here .
  • if the lobbyExists function is only iterating through an array, then there's no need to treat it as asynchronous and use a callback. synchronous callbacks are a thing, and are perfectly fine to use, but it's something to be aware of as you continue to develop.

with all that being said, these are the changes that I would suggest:

function doesLobbyExist(a, callback) {
    lobbyExists(a, function(random_data) {
        //possibly do other things to random_data...

        callback(random_data);
    });
}

function lobbyExists(a, callback) {
    for(var i = 0; i < lobbies.length; i++) {
        var l = lobbies[i];

        if(l.lobbyName === a) {
            console.log(a + " exists!");

            callback(true);

            return;
        }
    }

    callback(false);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM