Obtengo todos los precios por consulta ( req.query.currency ) -
// Gets currency column by query from crypto DB table const allPrices = await Crypto.findAll({ attributes: [req.query.currency, 'createdAt'], order: [['createdAt', 'ASC']] });Los primeros 2 objetos en la matriz, por ejemplo:
Crypto { dataValues: { ETHUSD: '4181.1015879408815', createdAt: 2021-10-21T08:16:37.000Z }, _previousDataValues: { ETHUSD: '4181.1015879408815', createdAt: 2021-10-21T08:16:37.000Z }, _changed: Set(0) {}, _options: { isNewRecord: false, _schema: null, _schemaDelimiter: '', raw: true, attributes: [ 'ETHUSD', 'createdAt' ] }, isNewRecord: false } Crypto { dataValues: { ETHUSD: '0', createdAt: 2021-10-21T08:17:16.000Z }, _previousDataValues: { ETHUSD: '0', createdAt: 2021-10-21T08:17:16.000Z }, _changed: Set(0) {}, _options: { isNewRecord: false, _schema: null, _schemaDelimiter: '', raw: true, attributes: [ 'ETHUSD', 'createdAt' ] }, isNewRecord: false } Tengo 3 opciones en req.query.currency - BTCUSD, ETHUSD, LTCUSD.
Obtendré uno de ellos cuando envíe la solicitud, cada uno representa la diferencia entre el primer valor, luego necesito agregarles el primer valor de esta manera:
const toClient = allPrices.map((item, index) => { if (index === 0) { return item; } return { ...item, price: (+allPrices[0] + +item).toString(), } });Los primeros 2 objetos en la matriz -
Crypto { dataValues: { ETHUSD: '4181.1015879408815', createdAt: 2021-10-21T08:16:37.000Z }, _previousDataValues: { ETHUSD: '4181.1015879408815', createdAt: 2021-10-21T08:16:37.000Z }, _changed: Set(0) {}, _options: { isNewRecord: false, _schema: null, _schemaDelimiter: '', raw: true, attributes: [ 'ETHUSD', 'createdAt' ] }, isNewRecord: false } { dataValues: { ETHUSD: '0', createdAt: 2021-10-21T08:17:16.000Z }, _previousDataValues: { ETHUSD: '0', createdAt: 2021-10-21T08:17:16.000Z }, _changed: Set(0) {}, _options: { isNewRecord: false, _schema: null, _schemaDelimiter: '', raw: true, attributes: [ 'ETHUSD', 'createdAt' ] }, isNewRecord: false, price: 'NaN' } ¿Cómo puedo obtener un valor en el price: 'NaN' ?
No puedo hacerlo así -
return { ...item, price: (+allPrices[0].BTCUSD + +item.BTCUSD).toString(), } Porque no sé cuál será la moneda. Así que necesito una forma de poner el req.query.currency aquí - (+allPrices[0]. BTCUSD + +item. BTCUSD ) ¿Cómo puedo hacer eso?