2015-05-04 108 views
0

我正在用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

是的,其他用戶缺少更新權限可能是這裏的問題。從那裏,你有兩個選擇:

  1. 使用服務器的方法做了更新,你像你說的(推薦)
  2. 如果你信任你的用戶(在大多數情況下,在我的愚見:不要」 t),您可以allow根據您的需要更新Meteor.users的權限。那麼你應該能夠在客戶端保持更新呼叫。
+0

然後我打算把它做到服務器端。 但是,我很好奇:是否有一個優勢,讓用戶做到這一點的服務器端?程序設計的東西?還是不存在? 即使對於除用戶()之外的其他集合,例如,爲什麼我應該讓我的用戶通過上傳圖像來更改我的頭像集合,而不是通過傳輸服務器端來以更安全的方式進行操作? 寫我的問題,我想答案是:讓行動客戶端允許分裂工作,並允許服務器「休息」,或者至少少工作。但它是唯一的目的嗎? –

+0

我想如果你只在可信任的人(即:朋友)中使用那個應用程序,那麼允許客戶端直接更新其他用戶使得代碼更清晰並且不會增加複雜性:你的邏輯不會在服務器和客戶端文件之間劃分,不需要再次檢查異常...另外,我猜它*應該*更好,因爲你只有一個異步函數...(當然仍然要求服務器) – SylvainB

+0

謝謝你的幫助隊友! –