Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array of objects results in array containing array contain object #275

Closed
j-d-carmichael opened this issue May 24, 2020 · 2 comments
Closed
Labels
question Qustions about how to use stuff

Comments

@j-d-carmichael
Copy link

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

@j-d-carmichael j-d-carmichael added the bug Something isn't working label May 24, 2020
@hasezoey hasezoey added question Qustions about how to use stuff and removed bug Something isn't working labels May 24, 2020
@hasezoey
Copy link
Member

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

@j-d-carmichael
Copy link
Author

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[];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Qustions about how to use stuff
Projects
None yet
Development

No branches or pull requests

2 participants