2015-08-08 70 views
0

我正在做一個應用程序,它使用帳戶包和Facebook的圖形API。具體的朋友api。朋友API返回所有使用該應用程序的Facebook朋友。問題是它返回的是Facebook的ID,而賬戶包生成特定於應用程序的ID。當我想從包含朋友信息的集合中檢索信息,但是與應用程序特定的ID一起存儲時,這是有問題的。我通過在集合中存儲fb id和帳戶id來解決此問題。流星的臉譜ID和帳戶ID

但我仍然不能更新基於他們的fb id的用戶數據,因爲只有使用特定於應用程序的id才允許更新。我想要的,但不允許:

UserData.update({fbId: friend.fbId},{$push: {some: data}}); 

我能想到的唯一的辦法是先獲取每個用戶ID,像這樣:

var friendId = UserData.findOne({fbId: friend.fbId})._id; 

這顯然不是一個很好的解決方案,因爲它需要每增加一個額外的數據庫調用。

有沒有辦法在創建時將帳戶ID設置爲等於facebook的ID?或者你有任何其他建議。

+0

是'UserData'自定義集合?如果您正在更新Meteor.users集合,更新時會出現什麼錯誤? – Xinzz

+0

@Xinzz UserData是一個自定義集合。如果嘗試使用fbId更新,我會得到以下錯誤:'未捕獲的錯誤:不允許。不受信任的代碼只能通過ID更新文檔。 [403]' – MoeRum

回答

0

擴展上面的評論:

MoeRum: @Xinzz UserData is a custom collection. If try updating with fbId I get the following error: Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]

那是因爲你想在客戶端更新。您只能在客戶端通過ID進行更新。只要您在服務器上執行操作,您嘗試執行的操作應該不會成爲問題。

從流星文檔(更多參考:http://docs.meteor.com/#/full/update):

The behavior of update differs depending on whether it is called by trusted or untrusted code. Trusted code includes server code and method code. Untrusted code includes client-side code such as event handlers and a browser's JavaScript console.

  • Trusted code can modify multiple documents at once by setting multi to true, and can use an arbitrary Mongo selector to find the documents to modify. It bypasses any access control rules set up by allow and deny. The number of affected documents will be returned from the update call if you don't pass a callback.

  • Untrusted code can only modify a single document at once, specified by its _id. The modification is allowed only after checking any applicable allow and deny rules. The number of affected documents will be returned to the callback. Untrusted code cannot perform upserts, except in insecure mode.