Compile-time error when using updateOne in MongoDB Node.js

3 days ago 4
ARTICLE AD BOX

I often get compile-time errors in TypeScript.

Example:

const response = await userModel.updateOne({ firstName:updatePayLoad.data?.firstName, lastName:updatePayLoad.data?.lastName, password:hashedPassword }

Error:

[{ "resource": "/D:/Pratice/Paytm/backend/src/router/user.ts", "owner": "typescript", "code": "2769", "severity": 8, "message": "No overload matches this call.\n Overload 1 of 2, '(filter: _QueryFilter<{ email: string; firstName: string; lastName: string; password: string; createdAt: NativeDate; updatedAt: NativeDate; }>, update: UpdateWithAggregationPipeline | UpdateQuery<...>, options?: (UpdateOptions & MongooseUpdateQueryOptions<...>) | ... 1 more ... | undefined): Query<...>', gave the following error.\n Argument of type '{ firstName: string | undefined; lastName: string | undefined; password: string; }' is not assignable to parameter of type '_QueryFilter<{ email: string; firstName: string; lastName: string; password: string; createdAt: NativeDate; updatedAt: NativeDate; }>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.\n Type '{ firstName: string | undefined; lastName: string | undefined; password: string; }' is not assignable to type '{ email?: StrictCondition<ApplyBasicQueryCasting<string>>; firstName?: StrictCondition<ApplyBasicQueryCasting<string>>; lastName?: StrictCondition<...>; password?: StrictCondition<...>; createdAt?: StrictCondition<...>; updatedAt?: StrictCondition<...>; }' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.\n Types of property 'firstName' are incompatible.\n Type 'string | undefined' is not assignable to type 'StrictCondition<ApplyBasicQueryCasting<string>>'.\n Type 'undefined' is not assignable to type 'StrictCondition<ApplyBasicQueryCasting<string>>'.\n Overload 2 of 2, '(filter: Query<any, any, {}, unknown, \"find\", Record<string, never>>, update: UpdateWithAggregationPipeline | UpdateQuery<{ email: string; firstName: string; lastName: string; password: string; } & DefaultTimestampProps>, options?: (UpdateOptions & MongooseUpdateQueryOptions<...>) | ... 1 more ... | undefined): Query<...>', gave the following error.\n Object literal may only specify known properties, and 'firstName' does not exist in type 'Query<any, any, {}, unknown, \"find\", Record<string, never>>'.", "source": "ts", "startLineNumber": 98, "startColumn": 38, "endLineNumber": 98, "endColumn": 47, "modelVersionId": 1417, "origin": "extHost1" }]

Here is Zod validation schema:

export const updateValidation = z.object({ firstName:z.string(), lastName:z.string(), password:z.string() })
Read Entire Article