JRedford
JRedford

Reputation: 106

How to update nested user collection

I've been trying to figure this out for quite a while now and feel like I've tried everything.

I have a nested collection under users called details. I had no issues updating the details fields from the client but I obviously want to run the updates through the server for security.

Here's my server code:

//server code
Meteor.methods({
  updateProfile : function() {
    Meteor.users.update({ _id: Meteor.userId() }, { $set: { "details.phoneNumber" : phoneNumber }
    });
  }
});

And my client code:

Template.userEdit.events({
  'submit updateProfile' : function(e, t){
    e.preventDefault();
    var firstName = e.target.phoneNumber;
    Meteor.call('phoneNumber');
  }
 });

For now I am publishing/subscribing to the entire users collection:

// Server
Meteor.publish("allUserData", function () {
  return Meteor.users.find();
});

// Client
Tracker.autorun(function () {
  Meteor.subscribe("allUserData");
});

Upvotes: 0

Views: 39

Answers (1)

forallepsilon
forallepsilon

Reputation: 429

Your server method is called "updateProfile", but you call "phoneNumber" on the client. Meteor methods are called as follows, in your case:

Meteor.call("updateProfile", phoneNumber);

And your server method must accept an argument as input:

Meteor.methods({
  updateProfile : function(phoneNumber) {
  //...
  }
});

Upvotes: 2

Related Questions