openexchangerates / money.js

money.js is a tiny (1kb) javascript currency conversion library, for web & nodeJS
http://openexchangerates.github.io/money.js
MIT License
1.46k stars 127 forks source link

No matter what, always ends in error #17

Open shelleyp opened 10 years ago

shelleyp commented 10 years ago

Trying various functions, they all return an error. Tried in client, and in a Node app. Error only says "fx error".

LinoleumKnife commented 9 years ago

I am assuming you are trying something like: fx.convert(12.99, {from: "GBP", to: "HKD"}); and it is resulting in "fx error"

If you try something simple like: fx(10000); you should get a value back. If you do, then the issue is you probably need to set up the exchange rates: http://openexchangerates.github.io/money.js/#fx.rates

shelleyp commented 9 years ago

Thanks, I'll give that a try.

artworkjpm commented 9 years ago

I had the same error, so I put the rates in as stated in http://openexchangerates.github.io/money.js/#fx.rates However, these are old static rates, what I wanted to do was actually update my values with the current currency using the api. I have wired up the api: $.getJSON( // NB: using Open Exchange Rates here, but you can use any source! 'http://openexchangerates.org/api/latest.json?app_id=[hidden]', function(data) { // Check money.js has finished loading: if (typeof fx !== "undefined" && fx.rates) { fx.rates = data.rates; fx.base = data.base; } else { // If not, apply to fxSetup global: var fxSetup = { rates: data.rates, base: data.base } } });

Can you tell me if its possible to do this?

LinoleumKnife commented 9 years ago

@artworkjpm - If I were in your position, I would utilize the default fx.rates on initialization and then update them with this api request you make. This will prevent any fx errors occurring with trying to use the library without setting up the rates, the underlying issue reported here.

The only caveat is that, in between the initialization and the api success, if you are utilizing the library, your values will be inaccurate and you will have to update them after the api success.

There are a dozen ways to get around that issue, but I don't know enough about your environment to comment.

Hope that helps.

ofarukcaki commented 5 years ago

I still have the same error