tadasajon
tadasajon

Reputation: 14836

Meteor.js - what is the most convenient way to update a user profile?

I find that I use the user profile extensively. I would like to be able to do something like:

Meteor.user().profile.some_setting = 'something';
Meteor.user().update();

What is the most convenient way to update a user's profile?

Upvotes: 2

Views: 1047

Answers (1)

Kyll
Kyll

Reputation: 7139

Meteor.user() is a document, not a cursor. It's actually an alias for Meteor.users.findOne(this.userId).

You can either do so via a method call (server), or directly on the client.

Method call way:

//server code
Meteor.methods({
  updateProfile : function(newProfile) {
    if(this.userId)
      Meteor.users.update(this.userId, {$set : { profile : newProfile }});
  }
});

And on the client:

Meteor.call('updateProfile', myNewProfile);

I advise to do so through a server method, since the code runs in a cleaner environment.

If you want to do it directly on the client:

Meteor.users.update(Meteor.userId(), {$set : {profile : myNewProfile}});

(Meteor.userId() is an alias for Meteor.user()._id)
More infos on the doc!

Upvotes: 4

Related Questions