0

im wondering if i can get some help here, im not a skilled coder by far, but im trying to retrieve results outside the function and the result in log im getting is Undefined

var pricecrex;

getDataFromAPI("https://api.crex24.com/CryptoExchangeService/BotPublic/ReturnTicker?request=[NamePairs=BTC_WAGE]", 
    true, 
    function(data){
        var resultcrex = JSON.parse(data);
        if (resultcrex !== "undefined") {
            if (resultcrex) {
                var pricecrex = resultcrex.Tickers[0].Last
            }
            else {
                msg.reply("0")
            }
        }
    }
);

console.log(pricecrex);
  • 3
    `getDataFromAPI` fetches the data asynchronously, so you are logging `pricecrex` before it is assigned its actual value – Scriptim Oct 27 '18 at 16:29
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – nicholaswmin Oct 27 '18 at 16:35

3 Answers3

8

It is because Ajax requests are async. console.log() gets executed before response is received from request, and thus before setting value in pricecrex. So you were getting undefined.

var pricecrex;

getDataFromAPI("https://api.crex24.com/CryptoExchangeService/BotPublic/ReturnTicker?request=[NamePairs=BTC_WAGE]", 
true, function(data) {
        var resultcrex = JSON.parse(data);
        if (resultcrex !== "undefined") {
            if (resultcrex) {
                pricecrex = resultcrex.Tickers[0].Last;
                print(pricecrex);
            }
            else {
                msg.reply("0")
            }
        }
    }
);

function print(data) {
    console.log(data);
}
Jignesh M. Khatri
  • 1,407
  • 1
  • 14
  • 22
  • and there is no good reason to declare `var pricecrex;` twice. The second one should be a simple assignment – Matthias Herrmann Oct 27 '18 at 16:34
  • What if I not just want to log the data? What if I call the getDataFromAPI to get the data returned? Wouldn't I have to call the print function then to be able to get the data returned? – Whoozy Mar 04 '21 at 16:59
1

The nature of Javascript is continue running code once an asynchronous function has been started. So you run getDataFromAPI(), and then while that's running, the interpreter goes to the next piece of code, which is your console.log(pricecrex).

So you can either run the console.log(pricecrex) directly in the callback, function(data){}, or to keep things cleaner, wrap your console.log() within a function and call that function from within your callback.

Example:

let someVar;

someAsync('someurl.com', (data) =>{
    someVar = data;
    callTheConsole()
})

function callTheConsole(){
     console.log(someVar)
}
jpadd009
  • 45
  • 4
0

Instead of assigning the value to the variable. Pass it to another function. Thus the value passed to another function is not 'undefined'.

function validation(pricecrex){
    console.log(pricecrex);
}
getDataFromAPI("https://api.crex24.com/CryptoExchangeService/BotPublic/ReturnTicker?request=[NamePairs=BTC_WAGE]", 
    true, 
    function(data){
        var resultcrex = JSON.parse(data);
        if (resultcrex !== "undefined") {
            if (resultcrex) {
                var pricecrex = resultcrex.Tickers[0].Last;
                validation(pricecrex);
            }
            else {
                msg.reply("0")
            }
        }
    }
);

For more information, check out the below link. Detailed information with examples is available. How to return the response from an asynchronous call??