#Cast to ObjectId failed for value

3 messages · Page 1 of 1 (latest)

slim scroll
#

I'm having some trouble with a cast from my dto
This is my error:

[Nest] 18  - 10/04/2024, 9:42:58 AM   ERROR [ExceptionsHandler] Cast to ObjectId failed for value "{
  title: { EN: 'Languages', FR: 'Langages' },
  list: [
    '66cee42b6ffdcb28e58d522f',
    '66cf4456f61c345925c8bd96',
    '66fe4c0ae8174dcb67e4dce3',
    '66fe4c15e8174dcb67e4dce8',
    '66fe4c23e8174dcb67e4dced',
    '66fe4c2ce8174dcb67e4dcf2'
  ]
}" (type Object) at path "_id" for model "Language"

This a part of my dto:

  @IsNotEmpty()
  @ValidateNested()
  @Type(() => LanguageDto)
  languages: LanguageDto;

And this is the class dto for the languages:

class LanguageDto {
  @IsObject()
  @IsNotEmpty()
  title: {
    EN: string;
    FR: string;
  };

  @IsArray()
  @IsMongoId({ each: true })
  list: Array<Types.ObjectId>;
}

Do you have any tips ?

prisma hedge
#

the list contains string, not ObjectIds so your LanguageDto should be list : Array<string>, and then you should transform them using list.map(id => new ObjectId(id))

slim scroll