如何在现有的已连接文档中进行级联删除、更新和反应性更新?例如,我加入了以Posts为作者的Meteor.users集合和userId()集合。我可以在Posts集合上进行转换函数,以获取作者的用户数据,比如username,并在任何帖子上显示作者的username。问题是当用户更改他/她的username时,现有的帖子不会主动更新作者的username。删除父文档时,子文档仍然存在。我使用了流行的智能包,如publish-composite和collection-helpers,但问题仍然存在。有哪个流星开发专家能帮到我吗?谢谢。
发布于 2015-10-31 16:37:01
如果您想要使用集合钩子来解决这个问题,下面的伪代码应该可以让您运行:
// 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});
}https://stackoverflow.com/questions/33449481
复制相似问题