0

I have a problem with returning value of callback function. I have a main function with two functions.

The problem is that the main function returns "undefined". It is important for me to get the value from main function.

var rate = 0;


function main(sum, from, to) {


  var script = document.createElement('script');
  script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D" + from + to + "%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate");
  document.body.appendChild(script);


  function parseExchangeRate(data) {
    rate = parseFloat(data.query.results.row.rate, 10);
  }

  return rate * sum;
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • It seems your query returns an `undefined` – David Gourde Jul 13 '16 at 08:21
  • 1
    You have two problems. The big one is described in the duplicate. The second one is that `parseExchangeRate` is a locally scoped function to `main` and not a global, so the JSONP script won't be able to find it. – Quentin Jul 13 '16 at 08:21
  • I.e: Move the parseExchangeRate outside main and change `rate = parseFloat(data.query.results.row.rate, 10);` to `document.getElementById("rateContainer").innerHTML=parseFloat(data.query.results.row.rate, 10);` – mplungjan Jul 13 '16 at 08:24

0 Answers0