如何正确处理多种类型的 joinByArrayReverse?

How to properly handle joinByArrayReverse with multiple types?

我的作品类型 project 有一个 _author,可以是两种不同的类型:

module.exports = {
  extend: 'apostrophe-pieces',
  name: 'project',
  label: 'Project',
  pluralLabel: 'Projects',
  addFields: [
    {
      label: 'Creator',
      help: 'Person or collective who created the project.',
      name: '_author',
      idsField: 'creatorIds',
      type: 'joinByArray',
      withType: [
        'person',
        'organization'
      ],
      filters: {
        projection: {
          _url: 1,
          title: 1,
        }
      },
      required: true
    },
    ...

这很好用。

另一方面,我希望为作者创建反向关系,以便有办法获得他们的项目。

module.exports = {
  extend: 'apostrophe-pieces',
  name: 'person',
  label: 'Person',
  pluralLabel: 'People',
  addFields: [
    {
      name: '_projects',
      reverseOf: '_author',
      idsField: 'creatorIds',
      type: 'joinByArrayReverse',
      withType: 'project',
      filters: {
        projection: {
          _url: 1,
          title: 1,
        }
      },
    },
    ...

编译正常,但是一旦加载站点,就会抛出此错误:

Error: I think you forgot to set idField or idsField, or you set the wrong one (use idField for byOne, idsField for byArray)

那么处理这种情况的适当策略是什么?

贡献很晚,但这恰好发生在我身上。可以重现该问题,方法是首先将片段类型设置为 joinbyOne,创建一些片段,然后将连接切换为 joinByArray。

当我这样做时,我遇到了您突出显示的错误。我通过删除 reverseOf 字段

来修复它

你的情况:

  name: '_projects',
  idsField: 'creatorIds',
  type: 'joinByArrayReverse',
  withType: 'project',
  filters: {
    projection: {
      _url: 1,
      title: 1,
    }
  },

documentation 中,它声明该字段是可选的,只要只有一个与片段类型的连接。如果您有多个 person 片段类型的连接,此解决方案可能无效。我仍然不确定为什么删除它可以解决问题,但它确实有效。