简体   繁体   中英

Updating Meteor.users from client

I have a form that tried to update meteor.users with extra information about users with the following helper

Template.Profile.events({
  'submit form': function(e) {
    e.preventDefault();

    var post = {

        firstName: $(e.target).find('[name=firstname]').val()

    };

    Meteor.users.update( { _id: Meteor.userId() }, { $set: { 'firstName': post.firstName }} );

  }
});

however, i get update failed: Access denied

Another question is, I am wondering whether I should do extra update straight to Meteor.users collection or should I have a seperate collection to store these data.

thanks

Due to the fact that you are trying to set an attribute directly on the base user object, you are receiving the 'Access denied' error. According to the Meteor documentation for Meteor.users :

By default, the current user's username , emails , and profile are published to the client.

This means that you can update any of those user attributes, but if you want to add additional ones, it is best to add them to one of these already existing fields. I would suggest adding something like `firstName' to the profile attribute. In this case, your code would look something like this:

Meteor.users.update({_id: Meteor.userId()}, {$set: {'profile.firstName': post.firstName}});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM