1

i'm using Meteor with collection2 and I have an array that looks like this:

productTypes = ["string1", "string2", "string3", {Other: "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?

I've tried

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

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

Edit:

I am now storing it with this format:

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},
})

My App crashes. When I remove the line list: {type: Array} it is fine.

Is Array now allowed as a value of SimpleSchema?

1
  • Also, can anyone explain why I can't use {type: Array, blackbox: true} ? Whenever I use that it blows my App up, I think there is a concept here that i'm missing Commented Jun 24, 2016 at 0:15

1 Answer 1

1

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"}]

for example:

productTypes = { list: ["string1", "string2", "string3"], Other: "test"}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks that is a great idea, I was already deciding I probably needed to do something like that. Since it was so hard to do, I figured there was a reason it wasn't supported.
Just wanted to note, that by switching to productTypes = { list: ["string1", "string2", "string3"], Other: "test"} it actually ended up cleaning up my code a lot. Thanks again
Also, now that I switched it over I am having the same issue with declaring an Array in Simple Schema. Could you look at it? See my edit.
You should declare list: {type: [String]} to indicate that list is an array of strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.