Sequelize attributes in findOne returning all fields

Viewed 4620

I have

db.User.findOne({
  attributes: ['id', 'firstName', 'lastName', 'email', 'phoneNumber', 'createdAt', 'type', 'status'],
  where: {
    id: id
  }
}).then(function(dbUser) {
  console.log(dbUser);
});

And it's returning all of the fields, not just the ones I specify in attributes. What am I doing wrong?

3 Answers

According to the docs, you're doing absolutely nothing wrong. I'm seeing similar behavior. Sequelize seems to be going through some growing pains. :\

The code provided works with version 7 at least.

const user = await User.findOne({
    attributes : ['id','name','email','contact'],
    where: {email:req.body.email}
});

Response from postman

{
"status": "success",
"user": {
    "id": 1,
    "name": "admin",
    "email": "admin@eatanddrink.io",
    "contact": "0724466628"
 },
}
Related