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

148
Vistas
One line if/else in Javascript for Sequelize query

I have working if/else that looks like this:

 async invoke(request, response) {
        const { deleted } = request.query;
        let users = [];
        if (deleted === 'true') {
            users = await User.findAndCountAll({
                where: {
                    deletedAt: { [Op.not]: null }
                }
            });
        } else if (deleted === 'false') {
            users = await User.findAndCountAll({
                where: {
                    deletedAt: null
                }
            });
        }

        return response.send(users);
    }

I am trying to make it one liner but the problem is { deleted } returning "undefined" and "true", and I need it to return "true" and "false

async invoke(request, response) { const { deleted } = request.query;

    const deletedAt = deleted ? { [Op.not]: null } : null;

    const users = await User.findAndCountAll({
        where: {
            deletedAt
        }
    });

    return response.send(users);
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Assuming that the only two valid states for deleted are "true" and "false" (odd that it's a string), then since the only difference in the findAndCountAll calls is the value of deletedAt, you can use the conditional operator:

async invoke(request, response) {
    const { deleted } = request.query;
    let users = await User.findAndCountAll({
        where: {
            deletedAt: deleted === 'true' ? { [Op.not]: null } : null
// −−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        }
    });

    return response.send(users);
}
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