0

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

4

1 回答 1

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});
}
于 2015-10-31T16:37:01.783 回答