无法在API调用中设置对象的值

时间:2019-10-27 12:04:21

标签: javascript node.js json

我需要我的代码使用open-exchange-rates获取当前汇率,使用money.js转换货币并将返回值设置为JSON对象。但是问题在于,JSON对象保持不变,不会被修改:

 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;
    });         
 });

尝试承诺后更新: 仍然无法正常工作。

  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')
    })

  });

0 个答案:

没有答案
相关问题