Meteor: How do I update the Users collection to include a new attribute in an object / dictionary?

I tried to search for the answer all the way, but I can not get it to work. I am using Meteor with Cordova to create a mobile application.

I want to add an attribute to my Users collection (the one that Meteor creates when I logged in). i.e. For example, I want to add {currentHotel: "Something"} to my db.Users collection.

I'm trying to do it right, publish - Sign the path. Using Meteor.methods has been cited for not being good for real-time applications. In any case, I want to understand how to update the Users collection using Publish-Subscribe.

//On the server, I did Meteor.publish("userData", function () { return Meteor.users.find({_id: this.userId}, {fields:{services: 1, currentHotel: 1}}); }); 

So, the currentHotel field should be available on the client side. Now update the currentHotel field:

 //On the client I do Meteor.subscribe('userData'); var user = Meteor.users.findOne({"_id": Meteor.userId()}); Meteor.users.update({_id: user._id}, {$set:{currentHotel:'something'}}); //I tried this too //Meteor.users.update({ _id: Meteor.userId() }, {$set: }); 

In the browser console, I see "currentHotel" and "services" just fine, which means that working with publish-subscribe works. However, I cannot update the currentHotel. I get Access Denied. Why is this?

Also, if the "currentHotel" property does not exist in the collection at all, how can I add it using a similar subscription publication? Can I publish a property that is not there and allow the client to subscribe and add this property?

I mentioned the docs Meteor, this , and this , but it still can't seem like it works !: - (

Thanks in advance!

+8
collections mongodb meteor publish-subscribe meteor-accounts
source share
1 answer

You should not change the root fields of the user object:

 Meteor.users.update(user, {$set: {"profile.currentHotel": "something"}}); 

More details here: http://docs.meteor.com/#/full/meteor_users

EDIT: This answer has become irrelevant, as shown in the latest documentation: https://guide.meteor.com/accounts.html#dont-use-profile

This answer is more than 1 year :) As it turned out, forcing to write custom fields in the "profile" field as some serious security consequences (as I always thought), since it gives customers permission to change these subfields. So, yes, you can set fields in the root object, but keep in mind that fields that should not be changed by the client must be under the field without write permissions (otherwise this will fall under the same problem).

+7
source share

All Articles