首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >流星:反应更新,级联删除/更新。正规化与去甲基化

流星:反应更新,级联删除/更新。正规化与去甲基化
EN

Stack Overflow用户
提问于 2015-10-31 08:10:43
回答 1查看 81关注 0票数 0

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-31 16:37:01

如果您想要使用集合钩子来解决这个问题,下面的伪代码应该可以让您运行:

代码语言:javascript
复制
// 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});
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33449481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档