0

Im having a problem with getting a variable from a method to a other method

my code is like this

var fixed;

function getstats(){
cheerioReq("https://donate.renegadeline.com/", (a, body) => {
    var full = (body("h1").text());
    var fixed = full.slice(16, 24);
//var fixed will output a string from a website. 
});
}

2nd part:

function setstats(){
  bot.setPresence({
    game:{
      name: "value of 'fixed here'"
     }
   });
 }

I can't figure out how to get this done...

Thanks in advance!

xVanjaZ
  • 107
  • 1
  • 1
  • 9
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – dNitro Feb 16 '17 at 16:55
  • 1
    Either the above asynchronous issue, or simply the issue that you're using `var fixed =` instead of `fixed =`. – deceze Feb 16 '17 at 16:56

2 Answers2

1

Put a call to setStats() inside your AJAX success callback and pass through the fixed variable.

function getstats(){
  cheerioReq("https://donate.renegadeline.com/", (a, body) => {
    var full = (body("h1").text());
    var fixed = full.slice(16, 24);
    setStats(fixed);
  });
}


function setstats(val){
  bot.setPresence({
    game:{
      name: val
     }
   });
 }
mhodges
  • 10,938
  • 2
  • 28
  • 46
0

Remove var in 6th line, so that it acts as global

   var fixed;

    function getstats(){
    cheerioReq("https://donate.renegadeline.com/", (a, body) => {
        var full = (body("h1").text());
        fixed = full.slice(16, 24);
    //var fixed will output a string from a website. 
    });
    }

2nd part:

    function setstats(){
      bot.setPresence({
        game:{
          name: fixed
         }
       });
     }

Or you could also use window.fixed

Kiran Reddy
  • 734
  • 12
  • 28