0

I'm new to JS .

so i want to be able to convert currency , especially IDR to USD. and i want to use fixer.io . so here is my code :

function idr_to_usd(){
    var url = $.getJSON("http://api.fixer.io/latest?base=IDR&symbols=USD");
    var respon = url.responseJSON;
    var rates = respon.rates.USD;
    return rates;
}

it doesn't work , in inspector it say : filename.js:46 Uncaught TypeError: Cannot read property 'rates' of undefined

but i try the code directly in inspector console and it work. so where did i do wrong ? Thanks

taek
  • 1,009
  • 3
  • 11
  • 14

2 Answers2

0

This is your solution:

    var usd_rate = 0;
    function idr_to_usd(){
        $.getJSON('http://api.fixer.io/latest?base=IDR&symbols=USD' , function(json_data){
            var rates = json_data.rates.USD;
            console.log(rates);    // check the console here.
            usd_rate = rates;
        });
    }

    idr_to_usd();
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
  • Where is `idr_to_usd()` defined? – Mark Jul 22 '17 at 17:49
  • @Mark_M, updated the answer. – Himanshu Upadhyay Jul 22 '17 at 17:51
  • 1
    it return "undefined" – taek Jul 22 '17 at 17:52
  • 1
    It still won't work. You can't `return` from a callback like that. The return value of `idr_to_usd()` will be `undefined`. – Mark Jul 22 '17 at 17:52
  • Actually if you will console it, it will give you USD value. So actually there should be a global variable and USD value should be assigned to it. Let me update my answer for it. – Himanshu Upadhyay Jul 22 '17 at 17:53
  • 1
    It won't work. Now `idr_to_usd(); console.log(use_rate)` => `0`; You are ignoring the fact that `getJSON` is asynchronous. – Mark Jul 22 '17 at 17:56
  • You need to do something like: ` $.getJSON("http://api.fixer.io/latest?base=IDR&symbols=USD", callback); function callback(data) { var rate = data.rates.USD; console.log(rate)}` – Mark Jul 22 '17 at 17:58
  • Yes, now the updated answer works. Although there's no reason to assign the variable `usd_rate` because it won't be reliably available. – Mark Jul 22 '17 at 17:59
  • have you checked the code as I mentioned? @Mark_M – Himanshu Upadhyay Jul 22 '17 at 17:59
  • It is working for my every time. If its working for you guys, please up vote by accepting the answer. :-) Thanks in advance. – Himanshu Upadhyay Jul 22 '17 at 18:01
  • tried it return undefined then return the rates, anyway Thanks for trying to help @HimanshuUpadhyay :) – taek Jul 22 '17 at 18:04
  • It gives me `0.000075119` every time I execute the function. @taek – Himanshu Upadhyay Jul 22 '17 at 18:07
  • "gives" is not the same as "returns". The latter is an exact notion: the function returns `undefined`. It returns synchronously, while the rate will come asynchronously. BTW, this question has been asked numerous times, and all there is to know is in the linked answer that was used to close this question. – trincot Jul 22 '17 at 18:35
-3

Try this

function idr_to_usd(){
    var url = $.getJSON("http://api.fixer.io/latest?base=IDR&symbols=USD");
    var respon = JSON.parse(url.responseText);
    var rates = respon.rates.USD;
    return rates;
}

alert(idr_to_usd());

Url is returning response text that is a JSON object but that is stringify

You have to convert your json string to json object so I am doing like as following

var respon = JSON.parse(url.responseText);

Now from response you can get rates.

Run following snippet if you are firing ajax result

  function idr_to_usd(){
       $.getJSON("https://api.fixer.io/latest?base=IDR&symbols=USD",function(data){
   var USD=data.rates.USD;
        alert(USD);
})
};

idr_to_usd()
        
        
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Sourabh Somani
  • 2,138
  • 1
  • 13
  • 27