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

TypeScript Query.prototype.map() Mongoose 6 #11161

Closed
edgarbs1998 opened this issue Dec 31, 2021 · 2 comments
Closed

TypeScript Query.prototype.map() Mongoose 6 #11161

edgarbs1998 opened this issue Dec 31, 2021 · 2 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@edgarbs1998
Copy link

edgarbs1998 commented Dec 31, 2021

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
Type definition of Query.prototype.map() stills present, even thought it is no longer part of mongoose 6, at least based on documentation, it also reports this.productModel.find(...).lean(...).map is not a function.

If the current behavior is a bug, please provide the steps to reproduce.
https://gist.github.com/edgarbs1998/75342511e9cdf8eb4f6f9cdcffa19c19

What is the expected behavior?
I suppose the Query.prototype.map() function has been removed from Mongoose 6 since it no longer on the documentation, if so it should also be removed from types definitions.

However, I used to use it and so I would like to know what is the alternative to transform the query result as I like, and that can be chained as a query helper and not set on the schema, and that runs after any post hook. Well, the question is, what is the replacement for Query.prototype.map().

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 16.13.0
Mongoose: 6.1.4
MongoDB: 5.0.3

@Kamikadze4GAME
Copy link
Contributor

Hello @edgarbs1998

You need to change .map() to .transform()

See https://github.com/Automattic/mongoose/blob/master/CHANGELOG.md#600-rc0--2021-08-03

BREAKING CHANGE: rename Query#map() -> Query#transform() to avoid conflating with Array#map() and chaining #7951

Ref #11057

@edgarbs1998
Copy link
Author

edgarbs1998 commented Dec 31, 2021

Hello @Kamikadze4GAME,

Thank you for your quick reply.
I have't found that change because it is not stated on the Migrating from 5.x to 6.x guide, so I believe it should be referenced there so it won't cause confusion to other people like it did to me.

It looks like I am unable to use the .transform() because its type definition is not on the index.d.ts yet.
I am quite new to TypeScript so, is there a way to fix the following TS error? Because my TS configs do not allow me to just ignore it.

Property 'transform' does not exist on type 'Query<(Document<any, any, Product> & Product & { _id: ObjectId; })[], Document<any, any, Product> & Product & { _id: ObjectId; }, {}, Product>'.ts(2339)

My tsconfig.json file: https://gist.github.com/edgarbs1998/4dc70808d8bf25bd12a2162934efe50c

@IslandRhythms IslandRhythms added docs This issue is due to a mistake or omission in the mongoosejs.com documentation typescript Types or Types-test related issue / Pull Request labels Dec 31, 2021
@vkarpov15 vkarpov15 added this to the 6.1.6 milestone Jan 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

4 participants