简体   繁体   English

错误:回调不是函数(nodejs)

[英]Error: Callback is not a function (nodejs)

When I execute my code I keep getting the error 'TypeError: callback is not a function'. 当我执行代码时,我不断收到错误“ TypeError:回调不是函数”。
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. 我对Node.js还没有足够的经验,所以也许我忽略了一些显而易见的事情,但是我找不到此代码有什么问题。

client.get() is from this npm package. client.get()来自 npm包。 Maybe the error has something to do with that? 也许错误与此有关?

I would be very glad if you could help me out here. 如果您能在这里帮助我,我将非常高兴。

The caller of the callback (which is the client.get method in this case) decides what arguments are passed to the callback. 回调的调用方(在本例中为client.get方法)决定将哪些参数传递给回调。 You need to declare your callback to match what client.get says that it will pass to the callback. 您需要声明回调以匹配client.get所说的将传递给回调的内容。 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. 您可以将参数命名为任意名称(使用的名称无关紧要),但是它们将按照client.get决定的顺序获取值。

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: 在这种情况下, client.get使用您在代码中function(err, tweet, res)的三个参数client.get调用回调,因此您的callback必须是这样的:

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

     }
     });
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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