简体   繁体   English

使用回调“未定义不是函数”

[英]“undefined is not a function” using callbacks

I'm creating a twitter bot that is requesting from the same API, Wordnik, but each request is depending on the last request's results. 我正在创建一个从同一API Wordnik请求的Twitter机器人,但是每个请求都取决于上一个请求的结果。 So, I decided to try creating some code using callbacks to make sure that all of the information is returned from the API before the next function runs. 因此,我决定尝试使用回调创建一些代码,以确保在下一个函数运行之前从API返回所有信息。 I am having trouble setting it up, I have looked at many examples and I just cannot get the hang of it. 我在设置它时遇到了麻烦,我看了很多例子,但我实在无法理解。 (Sorry for the messy code). (对不起,代码混乱)。

The error I am getting right now is "undefined is not a function" in my function getWord() on thenRunThisFunction(getRhyme). 我现在遇到的错误是thenRunThisFunction(getRhyme)上我的函数getWord()中的“未定义不是函数”。 I'm wondering if I have a small error with the callbacks or if my whole approach to this problem is incorrect? 我想知道我的回调是否有小错误,或者我对这个问题的整体解决方法是否正确?

function runBot() {
var request = require('request');
var Twit = require('twit');
var async = require('async');
var T = new Twit({
    consumer_key:         '' // Your Consumer Key
  , consumer_secret:      '' // Your Consumer Secret
  , access_token:         '' // Your Access Token
  , access_token_secret:  '' // Your Access Token Secret
});

var WORDNIKAPIKEY = '';

// GLOBAL VARS
var randomWord; //get random word
var rhymingWord; //get rhyming word
var bogusDef; //get def of rhyming word
var tweet; // combined random and bogusdef 

function getWord(thenRunThisFunction){ 
    request('http://api.wordnik.com:80/v4/words.json/randomWord?hasDictionaryDef=false&minCorpusCount=0&maxCorpusCount=-1&minDictionaryCount=1&maxDictionaryCount=-1&minLength=5&maxLength=-1&api_key=' + WORDNIKAPIKEY, function (error, response, body1) {
        if (!error && response.statusCode == 200) {
        //console.log(body1) // Show the HTML for the Google homepage. 
        var pparsedData = JSON.parse(body1);
        console.log("Word: " + pparsedData.word);

        // set random word
        randomWord = pparsedData.word;
        thenRunThisFunction(getRhyme);
        }
    })
}

// Get the rhyming word
function getRhyme(thenRunThisFunction){
    request('http://api.wordnik.com:80/v4/word.json/' + randomWord + '/relatedWords?useCanonical=false&relationshipTypes=rhyme&limitPerRelationshipType=10&api_key=' + WORDNIKAPIKEY, function (error, response, body2) {
        if (!error && response.statusCode == 200) {
        //console.log(body2) // Show the HTML for the Google homepage. 
        var o = JSON.parse(body2);
        console.log("Rhyme: " + o[0].words[0]);

        // set rhyming word
        rhymingWord = o[0].words[0];
        thenRunThisFunction(getDef);
        }
    })
}

// GET THE SEXY DEFINITION BABY, BEACH BOD
function getDef(thenRunThisFunction){       
    request('http://api.wordnik.com:80/v4/word.json/' + rhymingWord + '/definitions?limit=200&includeRelated=true&sourceDictionaries=all&useCanonical=false&includeTags=false&api_key=' + WORDNIKAPIKEY, function (error, response, body3) {
        if (!error && response.statusCode == 200) {
        //console.log(body3) // Show the HTML for the Google homepage. 
        var newnew = JSON.parse(body3);
        console.log("Definition: " + newnew[0].text);

        // set definition 
        bogusDef = newnew[0].text;

        randomWord = randomWord.charAt(0).toUpperCase();
        tweet = randomWord + ": " + bogusDef;
        thenRunThisFunction(postStatus);
        }
    })
}   

function postStatus(){
    T.post('statuses/update', { status: tweet }, function(err, data, response) {
        if(err) {
            console.log("There was a problem tweeting the message.", err);
        }
    });
    console.log("status posted");
}
getWord();
}
runBot();

您没有将函数引用传递给getWord()。

I have no clue really what you're trying to accomplish, instead of going 我真的不知道你要完成什么,而不是去

thenRunThisFunction();
thenRunThisFunction();
thenRunThisFunction();
thenRunThisFunction();
thenRunThisFunction();

Just invoke them by their names, remove the argument from them 只需通过它们的名称调用它们,从它们中删除参数

getRhyme();
getDef();

What you're doing will never work, you're trying to call thenRunThisFunction as if it actually exists, it's an argument in your function that never gets served 您正在做的事情永远都行不通,您试图调用thenRunThisFunction就像它确实存在一样,它是函数中的一个参数,永远不会得到服务

Your method would work if it was like this: 如果这样,您的方法将起作用:

function runThisFunction(fnc) {
  fnc();   
}

function blah(thenRunThisFunction) {
    thenRunThisFunction(thing);
}

function thing() {
  console.log('Blah');
}

blah(runThisFunction);

But that's horrible and bad. 但这是可怕的和糟糕的。

You aren't passing anything to getWord at the end so thenRunThisFunction is literally undefined. 您最后没有传递任何内容给getWord,因此RunThisFunction实际上是未定义的。 Try passing a function to getWord like this getWord(function(){}). 尝试像这样将函数传递给getWord(getWord(function(){}))。 But in your case you want to pass whatever you want to run after get word. 但是在您的情况下,您想通过任何想传递的信息就可以通过。

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

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