我正在努力解决如何使用MeteorJs从2个相关表中检索数据的问题。
我有2个收藏(来自SimpleSchema)
users : {
_id : hdhdgehehehehe,
firstname: John,
branch: dhdhdhdhdhdhdhd
}
第二个系列
branches : {
{
_id : dhdhdhdhdhdhdhd,
description: admin
},
{
_id : eebdbbbdbdbdbdb,
description: management
}
}
在服务器/出版物上 我想列出所有具有分支描述的用户, 类似的东西:
users : {
_id : hdhdgehehehehe,
firstname: John,
branches : {
_id : dhdhdhdhdhdhdhd,
description: admin
}
}
请帮助...谢谢
答案 0 :(得分:0)
您可以通过将光标返回到数组中来发布多个集合中的字段。这是一个例子:
Meteor.publish('user.branches.info', function() {
//if no user is logged in stop the publication
if(!this.userId) this.stop();
//save the branch id as a variable
const branchId = Meteor.users.findOne(this.userId).branch;
//return an array holding the user data and the branch data
return [
Meteor.users.find(
{ _id: this.userId },
{ fields: { firstname: 1, branch: 1 }
),
Branches.find(
{ _id: branchId },
{ fields: { description: 1 }
);
]
});