【问题标题】:Meteor: Reactive update, cascaded delete/update. Normalizing vs DenormalizingMeteor:反应式更新,级联删除/更新。规范化与非规范化
【发布时间】:2016-01-31 16:12:41
【问题描述】:

如何在现有的joined文档中进行级联删除、更新和响应式更新?例如,我以userId() 为作者加入Posts 集合和Meteor.users 集合。我可以对Posts 集合执行转换功能以获取作者的用户数据,例如username 并在任何帖子上显示作者的username。问题是当用户更改他/她的username 时,现有帖子不会反应性地更新作者的username。当您删除父文档时,子文档仍然存在。我使用了流行的智能包,如publish-compositecollection-helpers,但问题仍然存在。任何专家流星开发可以帮助我吗?谢谢。

【问题讨论】:

  • 这个问题一般要回答。您需要更清楚地陈述您的问题,并可能将其缩小到一个主题。
  • 对不起,我把这个贴在流星论坛上,比较合适的地方。现在我正在查看这个名为 collection-hooks 的智能包。看起来它会解决我的问题。

标签: meteor meteor-helper meteor-collections


【解决方案1】:

如果你想使用 collection-hooks 来解决这个问题,下面的伪代码应该可以帮助你:

// run only on the server where we have access to the complete dataset
if (Meteor.isServer) {
  Meteor.users.after.update(function (userId, doc, fieldNames, modifier, options) {
    var oldUsername = this.previous.username;
    var newUsername = doc.username;
    // don't bother running this hook if username has not changed
    if (oldUsername !== newUsername) {
      Posts.update({
        // find the user and make sure you don't overselect those that have already been updated
        author: userId, 
        authorUsername: oldUsername
      }, {$set: {
        // set the new username
        authorUsername: newUsername
      }}, {
        // update all documents that match
        multi: true
      })
    }
  }, {fetchPrevious: true});
}

【讨论】:

  • 太棒了。我得到了发布复合、集合挂钩和集合助手都很好地协同工作。但我不明白为什么我不能在 FS.Collection 上使用集合助手或转换函数。 CollectionFS 文档说您可以在基础 Mongo.Collection 上执行此操作。认为 MyCollectionFS.files.helpers({}) 会起作用,但事实并非如此。无论如何,非常感谢。
  • 没问题。如果当前答案对您有用,您可以通过将其标记为接受来让所有人知道。不要犹豫,也可以针对 CollectionFS 问题发布另一个问题。
  • 我为此发布了问题,这里是链接stackoverflow.com/questions/33444401/…
猜你喜欢
  • 2016-05-13
  • 2018-06-06
  • 2017-06-17
  • 2023-03-13
  • 2011-09-24
  • 1970-01-01
  • 2016-10-22
  • 2017-10-23
  • 2017-03-01
相关资源
最近更新 更多