0

I just upgraded to Mongoose 5.12 from 5.11 and am using Typescript. I have this schema

const MyFileSchema = new Schema<IMyFile>({
    objectID: { type: String, required: true },
    attachments: { type: Array, required: false }
}, { collection: 'my_file' });

Now this findOneAndUpdate is suddenly failing to compile ...

await MyFile.findOneAndUpdate(
    { objectID },
    {
        $push: {
            attachments: { fileName: fileName, id: fileID },
        },
    },
);

complaining about ...

Type '{ attachments: { fileName: string; id: string; }; }' is not assignable to type 'PushOperator<_AllowStringsForIds<LeanDocument<any>>>'.
Type '{ attachments: { fileName: string; id: string; }; }' is not assignable to type 'NotAcceptedFields<_AllowStringsForIds<LeanDocument<any>>, readonly any[]>'.
Property 'attachments' is incompatible with index signature.
Type '{ fileName: string; id: string; }' is not assignable to type 'undefined'.

Is there another way to write the above to avoid this compilation error that wasn't happening when I was using Mongoose 5.11?

Great Coder
  • 397
  • 3
  • 14
Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

0
await MyFile.findOneAndUpdate(
    objectID,
    {
      $push: {
        attachments: { fileName: fileName, id: fileID },
      },
    },
);

try with this It will work.

Note: You need to remove {} parenthesis from objectID search parameter.

Chandan
  • 11,465
  • 1
  • 6
  • 25
Prakash Harvani
  • 1,007
  • 7
  • 18