I have a Typegoose model (simplified)
export class SurveyClass extends BaseClass {
@prop({ required: true, default: '' })
public description!: string;
@prop({ required: true, default: [] })
public questions!: Array<any>;
}
The "questions" array is intended to receive chunks of JSON, for example:
{
id: "1869050556234435",
type: "feedback",
data: "the feedback data"
i18n: {
fr: {
data: "le feedback data"
}
}
}
...because the kind and number of these chunks are arbitrary, and many are more deeply nested, I've tried to use Array<any>
This works, but if I pass in data like this:
{
id: "1869050556234435",
type: "feedback",
data: "the feedback data"
i18n: {}
}
}
... then the "i18n" key and value is never set in the database. Those fields are "ignored" (though the others are saved.)
I have a few questions associated with this:
By default mongoose removes properties that it considers "empty" before insertion.
If the default "empty" behavior is not wanted:
minimize
.[]
and will also store it this way.required: true
to disallow empty strings.Also the the class in the question is not properly formed and requires option type
to be set.
See Quick-Start-Guide Caution Note:
type
has to be defined when working with Arrays, because Reflection only returns basic information. Look here for why
Likepublic: string[]
is in reflection only Array.
Note: i am not sure if the schema option minimize
will apply to nested schemas (all nested classes in typegoose are nested Schemas)