首页 > 解决方案 > 如何在 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"
}

标签: meteormeteor-publications

解决方案


几天前我遇到了类似的问题。提供的代码有两个问题。首先,使用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 } }
          );
        }
      }
    ]
  };
});

推荐阅读