带有字符串和对象的 Meteor collection2 数组

Meteor collection2 array with Strings and Object

我将 Meteor 与 collection2 一起使用,我有一个如下所示的数组:

productTypes = ["string1", "string2", "string3", {Other: "test"}]

基本上数组中会有 0 到 7 个字符串,其他:'Test' 可能存在也可能不存在

所以我正在尝试制作一个处理这种情况的模式。有没有办法告诉它数组中会有字符串和对象?

我试过了

const residentSchema = new SimpleSchema({
  productTypes: {type: [String, Object], optional: true, blackbox: true},
})

但这显然行不通,因为它需要一个字符串和一个对象。有谁知道我怎样才能使这项工作?提前致谢

编辑:

我现在以这种格式存储它:

productTypes = { 列表:["string1"、"string2"、"string3"]、其他:"test"}

但是当我添加这样的架构时:

const productTypeSchema = new SimpleSchema({
  list: {type: Array},
  other: {type: String}
})

const residentSchema = new SimpleSchema({
  productTypes: {type: productTypeSchema},
})

我的应用程序崩溃了。当我删除行列表时:{type: Array} 没问题。

现在允许数组作为 SimpleSchema 的值吗?

使用 collection2,您可以拥有基元数组或对象数组,但不能是混合数组。单从建模视角看真的很乱

我建议你重构数组,而不是:

productTypes = ["string1", "string2", "string3", {Other: "test"}]

例如:

productTypes = { list: ["string1", "string2", "string3"], Other: "test"}