RxDB如何从另一个集合中填充包含对象数组的数组。

4
我将尝试从另一个集合中填充id数组。
我的JsonSchema如下:
{
  version: 0,
  type: "object",
  properties: {
    id: {
      type: "string",
      primary: true
    },
    // This is populated as expected
    working: {
      type: "array",
      ref: "othercollection",
      items: {
        type: "string"
      }
    },
    // This is where I am having problems
    notWorking: {
      type: "array",
      items: {
        type: "object",
        properties: {
          // This property is not being populated
          problem: {
            type: "array",
            ref: "yetanothercollection",
            items: {
              type: "string"
            }
          }
        }
      }
    }
  }
}

根据https://pubkey.github.io/rxdb/population.html中的文档,我应该能够:

带嵌套引用的示例

const myCollection = await myDatabase.collection({
  name: 'human',
  schema: {
    version: 0,
    type: 'object',
    properties: {
      name: {
        type: 'string'
      },
      family: {
        type: 'object',
        properties: {
          mother: {
            type: 'string',
            ref: 'human'
          }
        }
      }
    }
  }
});

const mother = await myDocument.family.mother_;
console.dir(mother); //> RxDocument

带有数组的示例

const myCollection = await myDatabase.collection({
  name: 'human',
  schema: {
    version: 0,
    type: 'object',
    properties: {
      name: {
        type: 'string'
      },
      friends: {
        type: 'array',
        ref: 'human',
        items: {
            type: 'string'
        }
      }
    }
  }
});

//[insert other humans here]

await myCollection.insert({
  name: 'Alice',
  friends: [
    'Bob',
    'Carol',
    'Dave'
  ]
});

const doc = await humansCollection.findOne('Alice').exec();
const friends = await myDocument.friends_;
console.dir(friends); //> Array.<RxDocument>

我的问题是为什么我无法访问myDocument.notWorking[0].problem_

这是控制台的截图,可以帮助你更好地理解我的情况:

Screenshot of console

正如您所看到的,ingredients属性没有填充来自配料集合的数据(不在图片中)。然而,taxes属性已经填充。


你解决了这个问题吗? - Unforgiven
1
我明白了。我以为我已经回答过了,但显然我没有。我今天稍后会写一个答案。无论如何,我最终使用了ORM方法:https://rxdb.info/orm.html - Justin Taddei
1个回答

1

除非使用OEM方法,否则这是不可能的。

https://rxdb.info/orm.html

const heroes = await myDatabase.collection({
  name: 'heroes',
  schema: mySchema,
  methods: {
    whoAmI: function(otherId){
        // Return the item with id `otherId` from the other collection here
        return 'I am ' + this.name + '!!';
    }
  }
});
await heroes.insert({
  name: 'Skeletor'
});
const doc = await heroes.findOne().exec();
console.log(doc.whoAmI());


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接