-
Notifications
You must be signed in to change notification settings - Fork 92
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
class-transformer not working properly #169
Comments
Duplicate of #160 |
@necm1 It seems there is an issue with toObject, which we need to figure it out. |
I have spent sometime with this, and still no idea what's causing this issue. |
@necm1 Please how did you add |
You need to use the import {PaginateModel} from 'mongoose';
/**
* BookService constructor
*
* @constructor
* @param {PaginateModel<Book & Document>} book
* @param {CacheService} cacheService
* @param {AuthorService} authorService
*/
constructor(
@InjectModel(Book.name) private book: PaginateModel<Book & Document>,
private readonly cacheService: CacheService,
private readonly authorService: AuthorService
) {} @aravindnc some time passed - did you find any solution / fix for this issue? |
this solution is working for me. @Schema({
collection: 'services',
timestamps: true,
toJSON: {
virtuals: true,
transform: (doc, ret) => {
ret['id'] = ret['_id'];
if (ret['fee'] && ret['fee'].length > 0) {
ret['fees'] = ret['fee'][0]['ref'];
}
},
},
})
export class Service implements IService {
@Prop({
type: mongoose.Schema.Types.ObjectId
})
@Exclude()
_id: string;
@Prop({
type: mongoose.Schema.Types.ObjectId
})
id: string;
@Prop({ type: String })
service: string;
@Prop({
type: String,
enum: EServiceType,
})
type: EServiceType;
@Prop({
type: String,
enum: EServiceType,
})
vehicleType: EServiceType;
@Prop({
type: mongoose.Schema.Types.ObjectId,
ref: 'Service',
})
@Type(() => Service)
parent?: string | undefined;
@Exclude()
@Prop([
{
type: ServiceFee,
},
])
@Type(() => ServiceFee)
fee: ServiceFee[];
@Prop({
type: mongoose.Schema.Types.ObjectId,
ref: ServiceFeeComponent.name,
})
@Type(() => ServiceFeeComponent)
fees: ServiceFeeComponent[];
@Prop({
type: Date,
})
createdAt: Date;
@Prop({
type: Date,
})
updatedAt: Date;
@Prop({
type: String,
})
created_by: string;
@Prop({
type: String,
})
updated_by: string;
constructor(partial: Partial<Service>) {
Object.assign(this, partial);
}
}
export const ServiceSchema = SchemaFactory.createForClass(Service);
ServiceSchema.plugin(mongoosePaginate); public getServices = async (query: FilterQuery<Service>, options: PaginateOptions) => {
return this._serviceModel
.paginate(query, options)
.then((services: PaginateResult<Service>) => ({
...services,
docs: services.docs.map(service => plainToClass(Service, JSON.parse(JSON.stringify(service))))
}));
}; |
Hey!
I'm actually facing an issue using
class-transformer
using NestJS. I try to exclude some properties for my response, but getting following error:Method I'm using to create paginate:
The following entities
User
&Profile
is a normalScheme
which looks for example like this:How I create the response:
Even without using the
transform
object in mypopulate
I'm facing the same error.I've an example where it works using
findOne()
(Excluding properties works):The text was updated successfully, but these errors were encountered: