Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts def of updateOne&findOneAndUpdate returns FlattenMap instead of UpdateWriteOpResult, when lean() is used #13382

Closed
2 tasks done
simllll opened this issue May 6, 2023 · 2 comments · Fixed by #13389
Closed
2 tasks done
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@simllll
Copy link
Contributor

simllll commented May 6, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.1.0

Node.js version

16.20

MongoDB server version

5.x

Typescript version (if applicable)

4.9.5

Description

I tried upgrading to mongoose 7.x, but now I'm stuck with some typescript issues.
image

somehow updateOne returns a FlattenMap and not the ResultType defined in updateOne:
image

I would expect a UpdateWriteOpResult here.

Steps to Reproduce

my UserModel is defined as

const userSchema = new Schema({...});

export interface IDBUser { .. };

const DBUserModel: Model<IDBUser> = model<IDBUser>('User', userSchema);


just run updateOne on it and check the return type.

Expected Behavior

typescript should correctly return the UpdateWriteOpResult

@simllll
Copy link
Contributor Author

simllll commented May 6, 2023

Okay looking close at this, this is only happening on two places in this file, but works on 23 more.
And looking even closer: it turns out, there as a .lean() in the query. as this does not return any collection results, this is of course not required at this place. seems more correct without, but would be great to actually don't even allow .lean() on .updateOne query?

td;dr:
Solution: remove .lean()
image

@simllll simllll changed the title typescript definition of updateOne returns FlattenMap and therefore it's not possible to access e.g. modiefiedCount typescript definition of updateOne returns FlattenMap and therefore it's not possible to access e.g. modifiedCount May 6, 2023
@simllll simllll changed the title typescript definition of updateOne returns FlattenMap and therefore it's not possible to access e.g. modifiedCount typescript definition of updateOne returns FlattenMap and therefore it's not possible to access e.g. modifiedCount - when .lean() is to updateOne added May 6, 2023
@simllll simllll changed the title typescript definition of updateOne returns FlattenMap and therefore it's not possible to access e.g. modifiedCount - when .lean() is to updateOne added typescript definition of updateOne returns FlattenMap and therefore it's not possible to access e.g. modifiedCount - when .lean() is added to updateOne added May 6, 2023
@simllll simllll changed the title typescript definition of updateOne returns FlattenMap and therefore it's not possible to access e.g. modifiedCount - when .lean() is added to updateOne added typescript definition of updateOne returns FlattenMap and therefore it's not possible to access e.g. modifiedCount - when .lean() is added to updateOne May 6, 2023
@simllll simllll changed the title typescript definition of updateOne returns FlattenMap and therefore it's not possible to access e.g. modifiedCount - when .lean() is added to updateOne ts def of updateOne returns FlattenMap instead of UpdateWriteOpResult (update only when .lean() is added to updateOne) May 6, 2023
@simllll
Copy link
Contributor Author

simllll commented May 6, 2023

Okay, problem 1 solved, another one popped up, now .lean() cannot be removed because I'm using findOneAndUpdate()

image

it says again FlattenMap is returned, which does not have a lastErrorObject and value, it should correctly return ModifyResult instead.
Notice: I'm specifying rawResult: true to get this type of result. Am I missing some breaking changes regarding this property?

Update 1: Thinking about rawResult.. ppbl this infer that no mongoose magic is happen anyway, right? Removing lean() would solve my problem again..

@simllll simllll changed the title ts def of updateOne returns FlattenMap instead of UpdateWriteOpResult (update only when .lean() is added to updateOne) ts def of updateOne&findOneAndUpdate returns FlattenMap instead of UpdateWriteOpResult, when lean() is used May 6, 2023
@vkarpov15 vkarpov15 added this to the 7.1.1 milestone May 8, 2023
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label May 8, 2023
vkarpov15 added a commit that referenced this issue May 10, 2023
types: make `lean()` not clobber result type for `updateOne()`, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants