2015-08-08 74 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.