2017-11-10 9 views
0

Sequelize 4.22.6ExpressJSを使用します。Sequelize - JSONレスポンスでパスワードを返さない

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 
}); 

私はこの試みた:

私はこれを持つユーザーを作成するとき、私は非表示パスワードなどのフィールドをしたい

Sequelize: don't return password &このSequelize: Don't return password on Create

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

をしかし、私は印象を持ってinstanceMethods()メソッドは呼び出されません。

私はsequelizeと前に同じ問題を持っていた...

答えて

0

を私はこのinstanceMethodsではconsole.log()機能()を入れていると私は、コンソールでこのログを見ることはできません。

あなたmodels/user.jsファイル:

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

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

return User; 
}); 

次に、作成したユーザーオブジェクトのインスタンスメソッドを呼び出すことができます私のような何かを回避しました。

関連する問題