Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda