【问题标题】:Fetch records based on currency根据货币获取记录
【发布时间】:2019-12-29 18:00:24
【问题描述】:

我有一张门票表,每张门票都有价格。我已将数据库中的基数设置为美元,假设有人想从美国以外的国家购买机票,我想以当地货币向用户显示价格。什么是合适的方法。首先,我虽然可以在运行时将门票价格转换为当地货币,但此操作成本很高,因为门票数量可能很大。

对于我使用Open Exchange Rates API的货币转换,我需要根据req.query提供的货币提供价格。

我无法在此处显示实际代码,但我有一个示例:

const express = require('express');
const Product = require('../models/Product');
const { fetchRates } = require('../services/fx_rates')

const router = new express.Router();

router.post('/products', async (req, res) => {

    const product = new Product({
        ...req.body
    });

    try {
        await product.save();
        res.status(201).send(product);
    } catch (e) {
        res.send(400).send(e);
    }
});

router.get('/products', async (req, res) => {
    try {
        const products = await Product.find()
        // if (req.query.to) {
        //     for (let product of products) {
        //         fetchRates(product.price, 'USD', to)
        //             .then(price => {
        //                 product.price = price
        //             })
        //             .catch(e => console.log(e))

        //     }
        // }
        console.log(products)
        res.status(200).send({ products: products })
    } catch (e) {
        res.status(500).send();
    }
});

module.exports = router

【问题讨论】:

  • 请向我们展示您到目前为止所做的尝试?
  • @AndrewL64 请检查更新问题
  • 您是否尝试过在发送数据之前测试服务器端转换?您可以在服务器中使用 axios/fetch,就像在客户端使用它一样!
  • @MattiaRasulo 我想到了类似的方法,但后来认为在性能方面不会很昂贵吗?我决定只发送当前的转换率,客户将进行计算以显示相关货币的价格。
  • 当然,我也会这样做,但在你想避免的问题中,所以我提出了一种不同的方法! ??????

标签: javascript node.js express mongoose


【解决方案1】:

一种方法是获取所需当地货币相对于美元的兑换率,然后使用代码而不是 API 转换价格。您可以使用open exchange latest.json endpoint。 这是一个例子。

const rp = require('request-promise');

async function getConversionRate(localCurrency, base = 'USD'){
  const options = {
    uri: 'https://openexchangerates.org/api/latest.json',
    qs: {
      app_id: 'YOUR_APP_ID',
      base: base,
      symbols: localCurrency,
    },
    json: true
  };
  const result = await rp(options);
  /*
  {
    disclaimer: "https://openexchangerates.org/terms/",
    license: "https://openexchangerates.org/license/",
    timestamp: 1449877801,
    base: "USD",
    rates: {
        INR: 71.43
    }
  }
  */
  return result.rates[localCurrency];
}

router.get('/products', async (req, res) => {
    try {
        const [products, conversionRate] = await Promise.all([Product.find(), getConversionRate(req.query.to)])
        const convertedProducts = products.map(p => {
          p.price = p.price * conversionRate;
          return p;
        });
        console.log(convertedProducts)
        res.status(200).send({ products: convertedProducts })
    } catch (e) {
        res.status(500).send();
    }
});

module.exports = router;

【讨论】:

  • 我想到了这样的方法,但操作在性能方面的成本很高,所以我将只发送转换率,客户将相应地转换价格。
  • 您能否详细说明每个请求平均获取的产品数量?
【解决方案2】:

你可以使用 nodejs-currency-converter npm。

【讨论】:

  • 货币转换不是问题,我想以当地货币向用户展示产品。当它们是多条记录时,操作变得很耗时。
  • 然后你就可以使用客户端了。
猜你喜欢
  • 1970-01-01
  • 2012-06-12
  • 2017-11-29
  • 2013-08-18
  • 1970-01-01
  • 2017-09-22
  • 2014-12-07
  • 1970-01-01
  • 2011-05-16
相关资源
最近更新 更多