我有一个存储userId的消息中的消息列表。
Messages = new Mongo.Collection('messages');
// example of message data:
{
authorId: 'XFdsafddfs1sfd', // corresponding to a Meteor.userId
content: 'some Message'
}
而我发布消息和现有的用户。
Meteor.publish('messages', function() {
return Messages.find({});
});
Meteor.publish('users', function() {
return Meteor.users.find({});
});
目前我可以抓取消息列表并添加基于userIds的用户名。由于用户可以更改其用户名&配置文件信息,因此向用户数据添加用户数据(仅userIds)没有任何意义。
var messages = Messages.find({}).fetch();
var messages.forEach(function(message) {
message.username = Meteor.users.find(message.authorId).fetch() [0].username; // ugly
});
此代码有效,但涉及大量浪费的调用。我想知道如何以更好的方式实现这一点。
在流星中,什么是最有效的&最简洁的方式来将用户数据与包含userIds的集合配对?
您是否可以控制消息的创建方式或读取现有的mongo数据库? – sergserg
你看过这个吗?可能对你有意思。 https://www.discovermeteor.com/blog/reactive-joins-in-meteor/ – tomsp
我有控制数据库。 @tomsp我会看看,谢谢! – shmck