Define an attribute to contain an array of objects results in an array of an array containing a single object
import { getModelForClass, modelOptions, prop } from '@typegoose/typegoose';
import { v1 } from 'uuid';
export enum State {
Delivered = 'delivered',
Interacted = 'interacted',
Seen = 'seen',
Sent = 'sent',
}
class InteractionState {
@prop()
date: Date;
@prop({ required: true, default: State.Sent, enum: State, index: true })
state: State;
}
@modelOptions({
schemaOptions: {
collection: 'notification',
timestamps: true,
},
})
export class Notification {
@prop({ default: v1 })
_id: string;
@prop({ required: true, index: true })
interactionStateHistory: InteractionState[];
}
export const NotificationModel = getModelForClass(Notification);
After creating a doc and pulling backout of the db the result is:
{
"_id": "ed08fc60-9dd7-11ea-b20e-fbd93bc4fdff",
"interactionStateHistory": [
[{
"date": {
"$date": "2020-05-24T16:02:10.724Z"
},
"state": "sent"
}]
],
...
instead of:
{
"_id": "ed08fc60-9dd7-11ea-b20e-fbd93bc4fdff",
"interactionStateHistory": [{
"date": {
"$date": "2020-05-24T16:02:10.724Z"
},
"state": "sent"
}],
no
since 7.1 @prop can be used instead of @arrayProp, but the options are still needed, either type or items must be set, otherwise it will be an Mixed-Array, and from what i see these options are omitted
look here for details on why an option is still needed
PS: i know it dosnt yet stand in the documentation that one of these options are still needed, sorry
I just found the same answer too :)
Thanks for the speedy response! And awesome work on this package, massive time saver!
Adding items worked:
@prop({ required: true, index: true, items: InteractionState })
interactionStateHistory: InteractionState[];