I get all the prices by query (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']] });
The first 2 objects in the array for example -
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
}
I got 3 options in req.query.currency - BTCUSD, ETHUSD, LTCUSD.
I'll get one of them when I send the request, each represent the difference between the first value then I need to add the first value to them like this -
const toClient = allPrices.map((item, index) => {
if (index === 0) {
return item;
}
return {
...item,
price: (+allPrices[0] + +item).toString(),
}
});
The first 2 objects in the array -
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'
}
How can I get a value in the price: 'NaN'?
I can't do it like this -
return {
...item,
price: (+allPrices[0].BTCUSD + +item.BTCUSD).toString(),
}
Because I don't know what the currency is going to be.
So I need a way the put the req.query.currency here - (+allPrices[0].BTCUSD + +item.BTCUSD)
How can I do that?