6

I'm Trying to create a Mongo Schema, using nestjs/mongoose decorators, from the following class:

@Schema()
export class Constraint {
  @Prop()
  reason: string;

  @Prop()
  status: Status;

  @Prop()
  time: number;
}

The problem is Status is defined as followed:

export type Status = boolean | 'pending';

And I cannot figure out what to pass to the status's prop decorator, since I'm getting the following error:

Error: Cannot determine a type for the "Constraint.status" field (union/intersection/ambiguous type was used). Make sure your property is decorated with a "@Prop({ type: TYPE_HERE })" decorator

and { type: Status } doesn't work, since Status is a type and not a Class.

dod_moshe
  • 340
  • 2
  • 13

2 Answers2

1

Since status can be a boolean or a string it's a mixed type. So you can look into setting your type to Mixed

Sven Stam
  • 31
  • 4
1

I had the same problem, and thanks to @sven-stam, as he mentioned here, I implemented Mixed type like this:


import { Prop, Schema } from '@nestjs/mongoose';
import mongoose, {
  HydratedDocument,
  Schema as MongooseSchema,
} from 'mongoose';

export type UserDocument = HydratedDocument<User>;

@Schema()
class User {
  @Prop()
  username: string

  @Prop({default: false, type: MongooseSchema.Types.Mixed })
  paid: boolean | 'waiting'
}
Arda Örkin
  • 121
  • 3
  • 8