eRodY
eRodY

Reputation: 675

Error: Callback is not a function (nodejs)

When I execute my code I keep getting the error 'TypeError: callback is not a function'.
This is the relevant code:

exports.isVideo = function(tweetId, callback) {
  client.get('statuses/show/' + tweetId, function(err, tweet, res){
    if(tweet.extended_entities){
        if(tweet.extended_entities.media[0].type === 'video'){
            console.log('true');
            callback(true);
        } else {
            console.log('false');
            callback(false);
        }
    } else {
        console.log('false');
        callback(false);
    }
  });
}

This is in a different file (I required the first file at the top of this one, that's not the reason for the error):

ids.forEach(function(id){
    console.log(id.twitterId);
    twitterConverter.isVideo(id.twitterId, function(boolean){
        if(boolean){
            console.log('do something');
        } else {
            console.log('do nothing');
        }
    });
});

First of all, please don't just mark this as duplicate because there are other posts with similar titles. I looked through them, and the reason was usually that no callback function was passed, or too many parameters were passed, which isn't the case here.

I'm not that experienced with nodejs yet, so maybe I'm overlooking something obvious, but I can't find what's wrong with this code.

client.get() is from this npm package. Maybe the error has something to do with that?

I would be very glad if you could help me out here.

Upvotes: 0

Views: 2129

Answers (1)

Lakshmi Swetha G
Lakshmi Swetha G

Reputation: 2839

The caller of the callback (which is the client.get method in this case) decides what arguments are passed to the callback. You need to declare your callback to match what client.get says that it will pass to the callback. You can name the arguments anything you want (the names you use do not matter), but they will get values in the order that client.get decides.

In this case, client.get calls the callback with the three arguments you have in your code as in function(err, tweet, res).So your callback need to be something like this:

    callback(null,null,true);

Code is:

    exports.isVideo = function(tweetId, callback) {
    client.get('statuses/show/' + tweetId, function(err, tweet, res){
    if(tweet.extended_entities){
    if(tweet.extended_entities.media[0].type === 'video'){
        console.log('true');
        callback(null,null,true);

    } else {
        console.log('false');
        callback(null,null,false);

    }
    } else {
    console.log('false');
    callback(null,null,false);

     }
     });
     }

Upvotes: 1

Related Questions