流星:更新集合失败

问题描述:

我正在用Meteor.js建立一个好友列表管理系统。为此,我有一个用户列表,我可以点击邀请他们在我的朋友列表中。流星:更新集合失败

这是我的代码。我在e.target.id和e.target.name中分别获取其他用户_id和profile.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) 
    } 
    } 
    }); 
} 

的问题是,如果我正确地在我的好友列表和一个等待状态添加其他用户,我得到同样的错误时,抛出两次上的其他用户的两个更新。目前,我将_id和一些信息从所有用户的配置文件发布到客户端。我想这个问题来自于我可能不会重写客户端的另一个用户配置文件,但只能读取它。那么,我想我应该用一个方法调用来完成它的服务器端?

您能否确认我的问题,或向我解释如何进行?

谢谢你

是的,其他用户缺少更新权限可能是这里的问题。从那里,你有两个选择:

  1. 使用服务器的方法做了更新,你像你说的(推荐)
  2. 如果你信任你的用户(在大多数情况下,在我的愚见:不要” t),您可以allow根据您的需要更新Meteor.users的权限。那么你应该能够在客户端保持更新呼叫。
+0

然后我打算把它做到服务器端。 但是,我很好奇:是否有一个优势,让用户做到这一点的服务器端?程序设计的东西?还是不存在? 即使对于除用户()之外的其他集合,例如,为什么我应该让我的用户通过上传图像来更改我的头像集合,而不是通过传输服务器端来以更安全的方式进行操作? 写我的问题,我想答案是:让行动客户端允许分裂工作,并允许服务器“休息”,或者至少少工作。但它是唯一的目的吗? –

+0

我想如果你只在可信任的人(即:朋友)中使用那个应用程序,那么允许客户端直接更新其他用户使得代码更清晰并且不会增加复杂性:你的逻辑不会在服务器和客户端文件之间划分,不需要再次检查异常...另外,我猜它*应该*更好,因为你只有一个异步函数...(当然仍然要求服务器) – SylvainB

+0

谢谢你的帮助队友! –