流星collection2带有字符串和对象的数组 [英] Meteor collection2 array with Strings and Object
问题描述
我正在将Meteor与 collection2 一起使用,并且我有一个看起来像这样的数组:
i'm using Meteor with collection2 and I have an array that looks like this:
productTypes = ["string1", "string2", "string3", {Other: "test"}]
基本上,数组中会有0到7个字符串,其他:"Test"可能存在或可能不存在
Essentially there will be anywhere from 0 to 7 strings in the array, and Other: 'Test' may or may not be present
所以我正在尝试创建一个处理这种情况的模式.有没有办法告诉它数组中会有字符串和对象?
So i'm trying to make a schema that handles that case. Is there a way to tell it that there will be strings and an object within an array?
我尝试过
const residentSchema = new SimpleSchema({
productTypes: {type: [String, Object], optional: true, blackbox: true},
})
但是那显然是行不通的,因为它期望一个String和一个Object.有谁知道我该如何做这项工作?预先感谢
But that obviously won't work because it's expecting one String and one Object. Does anyone know how I can make this work? Thanks in advance
我现在以以下格式存储它:
I am now storing it with this format:
productTypes = {列表:["string1","string2","string3"],其他:"test"}
productTypes = { list: ["string1", "string2", "string3"], Other: "test"}
但是当我添加这样的架构时:
but when I add a schema like this:
const productTypeSchema = new SimpleSchema({
list: {type: Array},
other: {type: String}
})
const residentSchema = new SimpleSchema({
productTypes: {type: productTypeSchema},
})
我的应用程序崩溃.当我删除行列表时:{type:Array}很好.
My App crashes. When I remove the line list: {type: Array} it is fine.
现在可以将Array作为SimpleSchema的值了吗?
Is Array now allowed as a value of SimpleSchema?
推荐答案
使用collection2,您可以具有原始数组或对象数组,但不能具有混合数组.只是从建模角度来看,这真的很混乱.
With collection2 you can have an array of primitives or an array of objects but not a mixed array. Just from a modeling pov it's really messy.
我建议您重构数组,而不是:
I suggest you refactor your array, instead of:
productTypes = ["string1", "string2", "string3", {Other: "test"}]
例如:
productTypes = { list: ["string1", "string2", "string3"], Other: "test"}
这篇关于流星collection2带有字符串和对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!