As the title describe, it is always returns an empty array even when data is correct:
database.service.ts
async updateReply(id: string, attributes: Partial<ReplyDto>) {
console.log('Path request update reply with id: ' + id + ' and body:', attributes);
const reply = await this.replyModel.find({ _id: id });
// This was the previous way of fetching and update the object:
//
// const reply = await this.replyModel.findOneAndUpdate(
// { _id: new mongoose.Types.ObjectId(id) },
// { $set: attributes },
// { new: true }
// );
console.log('Reply updated: ', reply);
return reply;
}
replies.service.ts
async updateReply(id: string, body: UpdateReplyDto) {
console.log('Path request update reply with id: ' + id + ' and body:', body);
return await this.databaseService.updateReply(id, body);
}
replies.controller.ts
@Patch('/update/:id')
updateReplyById(@Param('id') id: string, @Body() body: UpdateReplyDto) {
console.log('Path request update reply with id: ' + id + ' and body:', body);
return this.repliesService.updateReply(id, body);
}
update-reply.dto.ts
import { IsArray, IsNumber, IsOptional, IsString } from "class-validator";
export class UpdateReplyDto {
@IsString()
@IsOptional()
languageCode: string;
@IsArray()
@IsNumber({ allowNaN: false}, { each: true })
@IsOptional()
stars: string[];
@IsArray()
@IsOptional()
keywords: string[];
@IsArray()
@IsOptional()
comments: string[];
}
reply.dto.ts
import { Expose, Transform, Type } from "class-transformer";
import mongoose, { Types } from "mongoose";
export class ReplyDto {
@Expose({ name: '_id' })
// makes sure that when deserializing from a Mongoose Object, ObjectId is serialized into a string
@Transform((value: any) => {
if ('value' in value) {
return value.value instanceof mongoose.Types.ObjectId ? value.value.toHexString() : value.value.toString();
}
return 'unknown value';
})
public id: string;
@Expose()
languageCode: string;
@Expose()
stars: string[];
@Expose()
keywords: string[];
@Expose()
comments: string[];
@Expose()
createdAt: Date;
@Expose()
updatedAt: Date;
}
Logs:
From Controller: Path request update reply with id: 62f6df243eb6410001621b90 and body: { languageCode: 'en', keywords: [ 'hey', 'you', 'friend', 'hello' ] }
From Service: Path request update reply with id: 62f6df243eb6410001621b90 and body: { languageCode: 'en', keywords: [ 'hey', 'you', 'friend', 'hello' ] }
From Database Service: Path request update reply with id: 62f6df243eb6410001621b90 and body: { languageCode: 'en', keywords: [ 'hey', 'you', 'friend', 'hello' ] }
.find Response: Reply updated: []