Hiding or adding properites to values/json/stringify?

2,013 views
Skip to first unread message

Eduard Ruzga

unread,
Jun 3, 2013, 8:51:24 AM6/3/13
to sequ...@googlegroups.com
Few questions.
I have a user model defined with password and email.
Also it has dynamic property realized trough getters from 1.7 version.
Property is a gravatar link generated based on email.

When I send json data of user to client two things go wrong.
First: Password is included in json data.
Second: gravatar link isn't.

So far I used a work around of defining toJSON in instanceMethods.
Code looks like this

Question is, what is official way to add or hide properties from values/json.

    var User = sequelize.define('user',
       
{
            email
: {type: Sequelize.STRING, length: 255, allowNull: false, unique: true},
            password
: {type: Sequelize.STRING, length: 20, allowNull: false}
       
},
       
{
            getterMethods
: {
                avatar_link
: function ()
               
{
                   
var hash = ...;
                   
return 'http://www.gravatar.com/avatar/' + hash;
               
}
           
},
            instanceMethods
: {
                toJSON
: function()
               
{
                   
var res = this.values;
                    res
.avatar_link = this.avatar_link;
                   
delete res.password;
                   
return res;
               
}
           
}
       
});

So as you see I remove password property and add avatar link property in redefined toJSON.
But its somewhat hacky, may break some things eventually.

So, is there official/better way to handle this?

Hugh Boylan

unread,
Jul 20, 2013, 12:26:51 PM7/20/13
to sequ...@googlegroups.com
I'm having a similar issue. Since eager loading with the 'include' parameter doesn't really work as advertised, I'm trying to chain a few queries together to add the required attributes to my instance before returning it.

Have you found a way to do this yet?

lucas abreu

unread,
Aug 24, 2014, 9:20:25 AM8/24/14
to sequ...@googlegroups.com
Did you find a better way to do it?

Mick Hansen

unread,
Aug 24, 2014, 2:07:58 PM8/24/14
to lucas abreu, sequ...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages