David Panart
David Panart

Reputation: 656

Meteor : updating collection fail

I'm building a friend-list managing system with Meteor.js.

For that, I have a list of users on wich I can click to invite them in my friend list.

Here is my code. I get the other user _id and profile.name respectively in the e.target.id and e.target.name.

Template.talkers.events({
  "click .addTalker": function(e, tmpl){
    /* Checking if there already is a pending request */
    var bool = false;
    for(var i = 0; i < Meteor.user().profile.friends.length; i++){
      if(Meteor.user().profile.friends[i].id == id){
        bool = false;
      }else{
        bool = true;
        break;
      }
    }

    /* If there isn't */
    if(!bool){

      /* I add the other user in my friend list with a pending status */
      Meteor.users.update({_id: Meteor.userId()}, {
        $push: {'profile.friends': {
          id: e.target.id,
          status: 0
          }
        }
      });

      /* Then, I add the request on the other user profile */
      Meteor.users.update({_id: e.target.id}, {
        $set: {'profile.friends': {
          id: Meteor.userId(),
          status: 2
          }
        }
      });

      /* And I create a new notification for the other user */
      var notifId = Meteor.users.findOne({_id: e.target.id}).profile.notifications.length;
      console.log(notifId + 1);
      Meteor.users.update({_id: e.target.id}, {
        $push: {'profile.notifications': {
          id: (notifId + 1),
          type: 1,
          img: null,
          link: "/profile"},
        }
      });
      alert("An invitation has been sent to " + e.target.name)
    }
  }
  });
}

The problem is that, if I correctly add the other user in my friend list with a pending status, I get the same error thrown two times for the two update on the other user. For the moment, I publish the _id and some informations from the profile of all users to the client. I guess the problem comes from the fact i might not be alowed to rewritte another user profile from client, but only to read it. Then, I guess I should do it server side with a method call ?

Can you confirm me the problem, or explain to me how to proceed ?

Thanks you

Upvotes: 0

Views: 147

Answers (1)

SylvainB
SylvainB

Reputation: 4820

Yes, the lack of update permissions on other users are probably the issue here. From there, you have two options:

  1. Use a server method to do the update for you like you said (recommended)
  2. If you trust your users (in most cases, in my humble opinion: don't), you can allow update permissions for Meteor.users under the conditions you desire. Then you should be able to keep your update calls in the client.

Upvotes: 1

Related Questions