javascript - "undefined is not a function" using callbacks -
i'm creating twitter bot requesting same api, wordnik, each request depending on last request's results. so, decided try creating code using callbacks make sure of information returned api before next function runs. having trouble setting up, have looked @ many examples , cannot hang of it. (sorry messy code).
the error getting right "undefined not function" in function getword() on thenrunthisfunction(getrhyme). i'm wondering if have small error callbacks or if whole approach problem incorrect?
function runbot() { var request = require('request'); var twit = require('twit'); var async = require('async'); var t = new twit({ consumer_key: '' // consumer key , consumer_secret: '' // consumer secret , access_token: '' // access token , access_token_secret: '' // 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 , 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 html google homepage. var pparseddata = json.parse(body1); console.log("word: " + pparseddata.word); // set random word randomword = pparseddata.word; thenrunthisfunction(getrhyme); } }) } // 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 html google homepage. var o = json.parse(body2); console.log("rhyme: " + o[0].words[0]); // set rhyming word rhymingword = o[0].words[0]; thenrunthisfunction(getdef); } }) } // 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 html 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 problem tweeting message.", err); } }); console.log("status posted"); } getword(); } runbot();
i have no clue you're trying accomplish, instead of going
thenrunthisfunction(); thenrunthisfunction(); thenrunthisfunction(); thenrunthisfunction(); thenrunthisfunction();
just invoke them names, remove argument them
getrhyme(); getdef();
what you're doing never work, you're trying call thenrunthisfunction
if exists, it's argument in function never gets served
your method work if this:
function runthisfunction(fnc) { fnc(); } function blah(thenrunthisfunction) { thenrunthisfunction(thing); } function thing() { console.log('blah'); } blah(runthisfunction);
but that's horrible , bad.
Comments
Post a Comment