如何从 Meteor 中的 ID 数组发布连接数据

How to Publish joined Data from Array of IDs in Meteor

我只想将发布的关系数据发布给客户端,但问题是我的关系数据字段是 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 的流星包,当我在 Children's find 中尝试了 Loop,我在 children 中得到了 undefined 即 below

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 } }
          );
        }
      }
    ]
  };
});