所以基本上我正在撰寫一個程式,我將值從一種貨幣轉換為另一種貨幣。我的函式看起來像這樣,我正在使用 OpenExchangeRates API 來獲取價值 Package doc is this。和 fx 是它的OXR子模塊
async function convertToAnotherCurrency(base, toConvert, amountToBeConverted) {
var convertedValue;
await oxr.latest(async () => {
fx.rates = oxr.rates;
fx.base = oxr.base;
convertedValue = await fx(amountToBeConverted).from(base).to(toConvert);
console.log(convertedValue); //this is logging expected response
});
console.log(await convertedValue); // this is showing undefined
}
任何人都可以幫助我找出我做錯了什么或幫助我提出建議嗎?
先感謝您。
uj5u.com熱心網友回復:
你得到這個問題是因為latest方法 fromopen-exchange-rates沒有給你一個承諾。下面將解決您的問題:
const fx = require("money");
const { promisify } = require("util");
const oxr = require("open-exchange-rates");
const oxrLatestPromisify = promisify(oxr.latest);
oxr.set({ app_id: "your_app_id" });
async function convertToAnotherCurrency(base, toConvert, amountToBeConverted) {
var convertedValue;
await oxrLatestPromisify();
fx.rates = oxr.rates;
fx.base = oxr.base;
convertedValue = await fx(amountToBeConverted).from(base).to(toConvert);
console.log(convertedValue);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/435669.html
上一篇:創建一個快速中間件來發送電子郵件
