'Sequelize exclude returns different value

So when I query without attribute: exclude... it returns me the correct user, but when I insert that in the query it returns a completly different user. Here is the code that I'm inserting.

const user = await Users.findOne(
    {
      attributes: { exclude: ["password", "admin", "createdAt", "updatedAt"] },
    },
    { where: { id: req.user.id } }
  );
  res.json({ user });

Any ideas why is this happening? Thanks in advance



Solution 1:[1]

You'r passing the where condition to the second paramter, but according to the docs: https://sequelize.org/docs/v6/core-concepts/model-querying-finders/#findone

Model.findOne only has one parameter which is the query options.

Move your where condition into the object for the first parameter.

const user = await Users.findOne({
  attributes: {
    exclude: ["password", "admin", "createdAt", "updatedAt"],
  },
  where: { id: req.user.id },
});

res.json({ user });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abir Taheer