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

I want to auto convert ObjectId to string #6996

Closed
lagmanzaza opened this Issue Sep 8, 2018 · 7 comments

Comments

Projects
None yet
5 participants
@lagmanzaza
Copy link

lagmanzaza commented Sep 8, 2018

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

What is the current behavior?
I use with Apollo server 2.0. when I want _id, but in response data, ObjectId is not converted to String.
What is the expected behavior?
How to auto convert ObjectId to String?
Please mention your node.js, mongoose and MongoDB version.
"mongoose": "^5.2.13",
node: 8
MongoDB: 3.6

@Lanfei

This comment has been minimized.

Copy link

Lanfei commented Sep 10, 2018

GraphQL have used a new method to serialize ID, the new method doesn't support ID in object type anymore, but following codes will help on this:

const mongoose = require('mongoose');

const ObjectId = mongoose.Types.ObjectId;

ObjectId.prototype.valueOf = function () {
	return this.toString();
};

Hope mongoose will support this feature soon :)

@vkarpov15

This comment has been minimized.

Copy link
Collaborator

vkarpov15 commented Sep 16, 2018

Interesting idea @Lanfei , will investigate that for a future release.

@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Sep 16, 2018

@Lanfei

This comment has been minimized.

Copy link

Lanfei commented Sep 17, 2018

@vkarpov15 Nice, thanks :)

@cannap

This comment has been minimized.

Copy link

cannap commented Oct 24, 2018

Hi any news about this?

@vkarpov15

This comment has been minimized.

Copy link
Collaborator

vkarpov15 commented Oct 29, 2018

@cannap #6912 should help with this, you'll be able to do:

mongoose.Schema.ObjectId.get(v => v != null ? v.toString() : v);

Does that API make sense?

@johnlife

This comment has been minimized.

Copy link

johnlife commented Nov 16, 2018

@vkarpov15, definitely, great idea, waiting for 5.4 patiently.
Thanks for great middleware.

@vkarpov15

This comment has been minimized.

Copy link
Collaborator

vkarpov15 commented Jan 3, 2019

5.4.0 is out, here's the workaround

@vkarpov15 vkarpov15 closed this Jan 3, 2019

@vkarpov15 vkarpov15 removed this from the 5.x Unprioritized milestone Jan 3, 2019

@Automattic Automattic locked as resolved and limited conversation to collaborators Jan 3, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.