Open x-shadow-x opened 2 years ago
I am encountering the same issue when using plainToClass method, all objectIds are changed. Any solution to the issue?
this happened after upgrading to mongoose 6.0.11.
This might be a similar issue https://github.com/typestack/class-transformer/issues/879
Still the same
Any workaround? I've the same issue when using class-transformer within NestJs. I'm using mongoose 6.1.7 and class-transformer 0.5.1
Not sure if it can helps other foxes, but I'm currrently using
@Transform((value) => value.obj._id.toString())
_id: ObjectId;
as workaround
@jaytonic It's good working for me. Thanks!
Just encountered this as well
+1 it returns new ObjectId instead of provided one
Previously we can omit @Type(() => String)
But I realised that it case this issue, we need to add @Type(() => String)
when ever we use @Transform
with plainToClass
to transform ObjectId
to String
@jaytonic and @tonynguyenit18 are right, @Type(() => String)
works a charm.
In my case this is a return type so transforming into a string is actually probably better than passing around a ObjectId because in any request it's going to need to come in as a string anyway.
It would be interesting to get insights from someone more versed in the workings of class transformer but something tells me this isn't an issue with the library but rather with our usage. If we put an objectId in, my guess is class transformer just creates a new one from the whole object (not just the string id) and mongoDB may not have a good way of dealing with this? @Type(() => String)
works though because ObjectId will have overridden the toString() function to just return the id. I'm making a lot of assumptions here though.
If you really did want to return the id, you could do this:
@Transform((value) => new Types.ObjectId(value.obj._id.toString()))
_id: Types.ObjectId;
I have tested it and it will create an objectId with the same Id, but think twice wether you really want to be doing this, it seems a bit pointless to me as the only advantage that you might have from using an ObjectId instead of the string is that you store generation time, but that's going to be reset every time you run this anyway.
There are workarounds for this, but isn't this problematic?
From practical stand of a point I believe this should produce correct output without any additional adjustments. Or at least throw an error, letting my know that this will produce unexpected output.
This affects Mongoose 6 and 7 based on my testing. Something's changed in v6 (broken) and then again in v8 (fixed). I can't put my finger on it as I don't see any change that'd apparently cause this behavior.
Description
object id is transformed into a completely different one when we add
excludeExtraneousValues: true
inplainToClass
functionMinimal code-snippet showcasing the problem
pls also check this repo to run the code and check the result. https://github.com/x-shadow-x/class-transform-plainToClass-issue
Expected behavior
should get the correct string of objectId.
Actual behavior