pirmax
pirmax

Reputation: 2153

Sequelize - Don't return password with JSON response

I use Sequelize 4.22.6 with ExpressJS.

I want hide password and others fields when I create an user with this:

models.User.create({
    firstName: 'Maxence',
    lastName: 'Rose',
    email: '[email protected]',
    password: 'azeaze',
}).then((result) => {
    res.json(result.toJSON()); // I don't want return "password" field
});

I tried this: Sequelize: don't return password & this Sequelize: Don't return password on Create

instanceMethods: {
    toJSON: () => {
        console.log("Console log here...");
    }
}

But, I have the impression instanceMethods() method isn't called.

I have put a console.log() function in this instanceMethods() and I can't see this log in console...

Upvotes: 0

Views: 1768

Answers (1)

Koray Gocmen
Koray Gocmen

Reputation: 715

I had the same issue before with the sequelize. I worked around with something like:

Your models/user.js file:

module.exports = function(sequelize, DataTypes) {
 const User = sequelize.define(
 'user',
 {
   // your columns here
 });

 User.prototype.toJSON = function(){
   console.log("Console log here...");
 };    

 return User;
});

Then you can call the instance method on a created user object.

Upvotes: 0

Related Questions