Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

283
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda