Typegoose: Array of objects results in array containing array contain object

Created on 24 May 2020  路  2Comments  路  Source: typegoose/typegoose

Versions

  • NodeJS: 12.16.3
  • Typegoose(NPM): 7.1.3
  • mongoose: 5.9.15
  • mongodb: 4.0.14:

What is the Problem?

Define an attribute to contain an array of objects results in an array of an array containing a single object

Code Example

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

Do you know why it happenes?

no

question

All 2 comments

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[];
Was this page helpful?
0 / 5 - 0 ratings

Related issues

c0ncentus picture c0ncentus  路  4Comments

hasezoey picture hasezoey  路  6Comments

AbderrazzakB picture AbderrazzakB  路  3Comments

wxs77577 picture wxs77577  路  5Comments

kerolloz picture kerolloz  路  6Comments