如何在 Meteor 中发布来自 ID 数组的连接数据



我只想将出版物的关系数据发布到客户端,但问题是我的关系数据字段array of ID's不同的集合,我尝试了不同的包,但都使用单个关系 ID 但不适用于Array of relational ID's,假设我有两个集合CompaniesMeteor.users下面是我的公司文档看起来像

{
_id : "dYo4tqpZms9j8aG4C"
owner : "yjzakAgYWejmJcuHz"
name : "Labbaik Waters"
peoples : ["yjzakAgYWejmJcuHz", "yjzakAgYWejmJcuHz"],
createdAt: "2019-09-18T15:33:29.952+00:00"
}

在这里你可以看到peoples字段包含用户ID作为数组,所以我如何将这个userId发布为用户文档,例如我尝试了最流行的流星包,名为publishComposit,当我在儿童发现中尝试循环时,我在儿童中找到了未定义,即下面

publishComposite('compoundCompanies', {
find() {
// Find top ten highest scoring posts
return Companies.find({
owner: this.userId
}, {sort: {}});
},
children: [
{
find(company) {
let cursors = company.peoples.forEach(peopleId => {
console.log(peopleId)
return Meteor.users.find(
{ _id: peopleId },
{ fields: { profile: 1 } });
})
//here cursor undefined
console.log(cursors)
return cursors
}
}
]
});

如果我在儿童发现中实现异步循环,我会收到以下代码的错误

publishComposite('compoundCompanies', {
find() {
// Find top ten highest scoring posts
return Companies.find({
owner: this.userId
}, {sort: {}});
},
children: [
{
async find(company) {
let cursors = await company.peoples.forEach(peopleId => {
console.log(peopleId)
return Meteor.users.find(
{ _id: peopleId },
{ fields: { profile: 1 } });
})
//here cursor undefined
console.log(cursors)
return cursors
}
}
]
});

上面代码中发生的错误是Exception in callback of async function: TypeError: this.cursor._getCollectionName is not a function我不知道我在这里到底做错了什么,或者实现包功能不符合预期,任何帮助都会被大大挪用

编辑:我想要的结果应该是完整的用户文档而不是 ID,无论它映射到同一个peoples数组中还是作为我只想要的另一个字段,如下所示

{
_id: "dYo4tqpZms9j8aG4C",
owner: "yjzakAgYWejmJcuHz",
name: "Labbaik Waters",
peoples: [
{
profile: {firstName: "Abdul", lastName: "Hameed"},
_id: "yjzakAgYWejmJcuHz"
}
],
createdAt: "2019-09-18T15:33:29.952+00:00"
}

几天前我遇到了类似的问题。提供的代码有两个问题。首先,使用async;它不是必需的,而是使事情复杂化。其次,publishComposite依赖于在其子游标中接收一个游标而不是多个游标才能正常工作。

下面是用于解决我遇到的问题的代码片段,希望您可以复制它。

Meteor.publishComposite("table.conversations", function(table, ids, fields) {
if (!this.userId) {
return this.ready();
}
check(table, String);
check(ids, Array);
check(fields, Match.Optional(Object));
return {
find() {
return Conversation.find(
{
_id: {
$in: ids
}
},
{ fields }
);
},
children: [
{
find(conversation) {
// constructing one big cursor that entails all of the documents in one single go
// as publish composite cannot work with multiple cursors at once
return User.find(
{ _id: { $in: conversation.participants } },
{ fields: { profile: 1, roles: 1, emails: 1 } }
);
}
}
]
};
});

最新更新