Gianmarco
Gianmarco

Reputation: 842

sequelize users.findOne using two parameters

I would like to use users.findOne method using two params like in the example below, not just with the id or name but with both

 const user = await Tags.findOne({
    where: { id: ID, name: NAME },
  });

Upvotes: 1

Views: 2221

Answers (2)

Engr.Aftab Ufaq
Engr.Aftab Ufaq

Reputation: 6394

here is an example . you have to user the Op from sequelize.

    const { Op } = require("sequelize");
    const isFoundNumber = await db.users.findOne({
          where: {
            [Op.or]: [
              {
                mobile_number: params.mobile_number,
              },
              { email: params.email },
           

 ],
      },
    });

    if (isFoundNumber != null) {
      let user = basicDetails(isFoundNumber);
      if (params.mobile_number == user.mobile_number) {
        reject({
          status: false,
          errorMessage: `Mobile Number ${params.mobile_number} already regstered`,
          data: basicDetails(isFoundNumber),
        });
      }

      if (params.email === user.email) {
        reject({
          status: false,
          errorMessage: `Email Account ${params.email} already regstered`,
          data: user,
        });
      }
    }

Upvotes: 0

Sven 31415
Sven 31415

Reputation: 1835

As far as I know you code should work, but you can try the following approach:

const user = await Tags.findAll({
  where: {
    [Op.and]: [
      { id: ID },
      { name: NAME }
    ]
  }
});

You can define operators for the where clause manually. For reference see https://sequelize.org/master/manual/model-querying-basics.html#applying-where-clauses

Upvotes: 1

Related Questions