Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
typescript/node/sequelize - How to add variables when getting dynamic value

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!