流星反应性地将某些字段从用户集合复制到自定义用户配置文件集合

问题描述 投票:1回答:1

在流星上,我安装了konecty meteor-user-presencelink)程序包,以为应用程序中的每个用户启用用户状态。该软件包在Meteor User集合上添加了其他字段(status,statusConnection)。在当前的应用程序设置中,我有一个名为UserProfiles的不同集合,该集合用于存储有关每个用户的其他信息。我使用了用户集合中的id作为UserProfiles集合的标识符,位于字段owner下。

是否有一种方法可以将用户集合中两个字段(状态和状态连接)的更新复制到自定义UserProfiles集合中?

mongodb meteor
1个回答
1
投票

我能够使用Atmospherejs的另一个名为collection-hooks {matb33:collection-hooks}的软件包来实现这一目标。我基本上将其添加到服务器上的main.js中:

Meteor.users.after.update(function (userId, doc, fieldNames, modifier) {
    UserProfiles.update({
      owner : doc._id
    }, {
      $set: {
        status : doc.status
      }
    }, {multi: true})
});

此程序包为集合添加了一个钩子。每次应用程序触发对Meteor.users集合的更新时(基本上是每次konecty meteor-user-presence更改status集合中的statusConnectionuser字段时,collection-hooks程序包都将更新动作和执行其他任务。该包装还具有其他有用的钩子,例如before.insertbefore.updatebefore.removeafter.insertafter.updateafter.remove

{multi: true}是必需的,才能使该行为应用于所有用户。我不知道这是否会对应用程序性能产生影响,我确定它会产生一些影响,尤其是当应用程序具有较大的用户群时,这种影响尤其明显。您应该仔细应用此解决方案。

这里是入门的好入门:A Look At Meteor Collection Hooks

© www.soinside.com 2019 - 2024. All rights reserved.