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

428
Vistas
node/sequelize/node with TS - how to cast to number and cast back to string inside a map with OR operator

I am trying to do addition of the currency prices which i am getting as an array of objects. But each time i receive this data, currency type can differ between 3 and stays same in the whole array of objects.

My problem here is that when i try to do the addition idk about the key beforehand which key(currency_type) it is. I have tried an approach using map and OR operator.

Here is the controller:

  const getHistorycalPrices = async (req: IGetHistorycalPricesRequest, res: IGetHistorycalPricesResponse) => {
    
      try {
        if (!req.query.currency) {
          res.status(400).send({
            success: false,
            message: "Could not get historycal prices",
          });
          return;
        }
    
        // Gets currency column by query from crypto DB table
        const allPrices = await Crypto.findAll({ attributes: [req.query.currency, 'createdAt'], order: [['createdAt', 'DESC']] });
    
        if (!allPrices) {
          res.status(400).send({
            success: false,
            message: "Could not get historycal prices",
          });
          return;
        }
    
        // Get first row from crypto DB table
        const firstRowPrices = await Crypto.findByPk(1);
    
        if (!firstRowPrices) {
          res.status(400).send({
            success: false,
            message: "Could not get first row prices",
          });
          return;
        }
    
        res.status(200).send({
          success: true,
          message: "Successfully retrieved historycal prices",
          data: allPrices.map((price) => ({
            cryptoPrices: (+price.BTCUSD + +firstRowPrices.BTCUSD).toString() ||
                          (+price.ETHUSD + +firstRowPrices.ETHUSD).toString() ||
                          (+price.LTCUSD + +firstRowPrices.LTCUSD).toString(),
            createdAt: price.createdAt.toDateString(),
          })),
        });
        return;
      } catch (e) {
    
        res.status(500).send({
          success: false,
          message: "Server error",
        });
        return;
      }
    };

Right now when I'm sending the data if it's the first line I'll get -

enter image description here

But if it's the ETHUSD OR the LTCUSD I'll get -

enter image description here

There is a problem with my OR operator in the allPrices.map but I can't understand what it is.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use the Optional chaining operator(?.).

allPrices.map((price) => ({
        cryptoPrices: (+price?.BTCUSD + +firstRowPrices?.BTCUSD ||
                      +price?.ETHUSD + +firstRowPrices?.ETHUSD ||
                      +price?.LTCUSD + +firstRowPrices?.LTCUSD).toString(),
        createdAt: price.createdAt.toDateString(),
      })),
    });

Another way: Instead of OR operator, try using the ternary operator.

allPrices.map((price) => ({
     cryptoPrices:(("BTCUSD" in price)?(+price.BTCUSD + +firstRowPrices.BTCUSD):
                  ("ETHUSD" in price)?(+price.ETHUSD + +firstRowPrices.ETHUSD):
                  (+price.LTCUSD + +firstRowPrices.LTCUSD)).toString(),
     createdAt: price.createdAt.toDateString(),
     })),
});
about 4 years ago · Juan Pablo Isaza Denunciar
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