Home > Blockchain >  Multiple field is not the array in nestjs mongoose Schema
Multiple field is not the array in nestjs mongoose Schema

Time:02-22

I wanted to connect multiple videos with foreign keys on my schema

  @Prop({
    type: mongoose.Schema.Types.ObjectId,
    ref: Video.name,
  })
  videos: Video[];

Btw, the result is not returning Array. I was returning 1 video (foreign key).

videos: "xxxxxx"

I want the below result

videos: ['xxxx', 'rrrr', 'yyyyy', ...]

How can I solve this issue?

CodePudding user response:

You can fix this issue easily.

  @Prop({
    type: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: Video.name,
        autopopulate: true,
      },
    ],
  })
  videos: Video[];

You have to define type as an array in the schema.

  • Related