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

145
Views
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 answers
Answer question

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 Report
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!