I need my code to get the current rates using open-exchange-rates, use money.js to convert the currency and set the returned value to a JSON object. But the problem is that the JSON object stays the same it does not get modified:
arr.prices.map(function(object) {
// get current rate
oxr.latest(function() {
fx.base = "USD";
fx.rates = oxr.rates;
// convert usd to zmw
let zmw = fx.convert(object["total"], { from: "USD", to: "ZMW" });
// set new value
object["total"] = zmw;
});
});
Update after trying a promise: Still not working.
arr.prices.map(function(object) {
let p = new Promise((resolve, reject) => {
oxr.latest(function() {
fx.base = "USD";
fx.rates = oxr.rates;
let zmw = fx.convert(object["total"], { from: "USD", to: "ZMW" });
if (zmw === undefined) {
reject('Fail')
} else {
resolve(zmw);
}
});
})
p.then((zmw)=>{
object["total"] = zmw;
}).catch((error) => {
console.log('Fail')
})
});