meteor - 如何在 Meteor 中发布来自 ID 数组的连接数据
问题描述
我只想将发布的关系数据发布给客户端,但问题是我的关系数据字段属于array of ID's
不同的集合,我尝试了不同的包,但都使用单个关系 ID 但不使用Array of relational ID's
,假设我有两个集合Companies
下面Meteor.users
是我的公司文件看起来像
{
_id : "dYo4tqpZms9j8aG4C"
owner : "yjzakAgYWejmJcuHz"
name : "Labbaik Waters"
peoples : ["yjzakAgYWejmJcuHz", "yjzakAgYWejmJcuHz"],
createdAt: "2019-09-18T15:33:29.952+00:00"
}
在这里你可以看到peoples
字段包含用户 ID 作为数组,所以我如何将这个用户 ID 发布为用户文档,例如我尝试了名为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 } }
);
}
}
]
};
});
推荐阅读
- mysql - 无论如何将单列数据显示到多列中
- java - Navigation Components: Switch Menu of Navigation Drawer (for logged in users)
- c# - IIS 请求计数限制与 IIS Express - Web API 核心
- python - 了解 scipy.integrate.odeint 中的时间步长
- javascript - 如何在 React 中禁用挂载时的初始悬停过渡?
- node.js - 如何将 Angular Universal 应用程序部署到 Node.js 生产服务器?
- java - Heroku 容器:释放抛出 UnhandledPromiseRejectionWarning:
- c - 为什么代码不是递归出来的?
- google-oauth - 与 Google 登录关联的帐户:有没有办法知道用户何时通过其 Google 帐户设置撤销/删除了访问权限?
- amazon-web-services - 如何在 AWS Lambda 中获取 Cognito 用户的身份 ID