Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelizejs findAll exclude field

I know with options.attributes you list the attributes that you want to select but Is there a way to only exclude a field ?

By now I worked it out with

User
  .findAll({order: [['id','DESC']]})
  .then(function(users) {
    users = users.filter(function(user){
      delete user.dataValues.password;
      return user;
    });
    return reply( ReplyUtil.ok(users) );
  })
  .catch(function(err){
    return reply( ReplyUtil.badImplementation(err) );
  });

BTW

I don't understand why on earth you should use user.dataValues.password if not delete doesn't work instead of simply user.password if I debug like this console.log('pass: ', user.password)I can see the password.

like image 980
Whisher Avatar asked Jul 28 '15 14:07

Whisher


People also ask

What does findAll return Sequelize?

The Sequelize findAll() method is used to query data from your SQL table to your JavaScript application. The method will return your table rows as an array of objects. The findAll() method can be called from a Model that represents the table in your database.

Why we use raw true in Sequelize?

The Sequelize setters and getters might not be used here. So setting raw to true provides me the desired output with a cleaner look & feel.


2 Answers

Yes it's possible to exclude fields, just like this :

User
  .findAll({
    attributes: {exclude: ['password']},
    order: [['id','DESC']]})
  .then( users => {
    return reply( ReplyUtil.ok(users) );
  })
  .catch( err => {
    return reply( ReplyUtil.badImplementation(err) );
  });

For more details see https://sequelize.org/master/manual/querying.html

like image 58
gduh Avatar answered Sep 30 '22 16:09

gduh


I know this is an old post. But I came here because of this same problem and I believe that more people will come. So, after studying some posts here in stackoverflow, I discovered that the simplest way is to use the select function to specify the fields we do not want to return. So its function would look like this:

User
  .findAll({order: [['id','DESC']]}).select('-password')
  .then(function(users) {
    return reply( ReplyUtil.ok(users) );
  })
  .catch(function(err){
    return reply( ReplyUtil.badImplementation(err) );
  });

Another way is to change the model (by code, I'm assuming you specified this model with mongoose or sequelize). You can specify the field like this:password: { type: String, select: false }. This select will cause the password, by default, not to be returned by any query in the database. Unless you use the previous function to add the password in the query (select ('+ password')).

AND

To answer your main question, Mongoose and Sequelize wraps all it's return values in a virtual object that contains meta data. If you have an object and you just want the undecorated you have to unwrap them, like that:

Model.findById(1).then(data => {
  console.log(data.get({ plain: true }));
});

And if you just want to print the object you can use the .toJSON:

Model.findById(1).then(data => {
  console.log(data.toJSON);
});

And if you just want the data and not the model instance, you can do this:

Model.findAll({
  raw: true
});
like image 36
Wagner de Andrade Perin Avatar answered Sep 30 '22 16:09

Wagner de Andrade Perin