#Date Fields Serialized as Empty Objects in NestJS Responses with Prisma and MongoDB

5 messages · Page 1 of 1 (latest)

static crane
#

Hello NestJS Community,

I am experiencing an issue with my NestJS application which uses Prisma with MongoDB. The problem arises with the serialization of date fields in API responses.

Problem Description:

In my NestJS application, I have Prisma models with DateTime fields. These fields are correctly stored and retrieved from MongoDB, and they appear as expected when logged in the console at both the service and controller levels. However, when these data objects are sent as responses to the client, the date fields are serialized as empty objects instead of proper date strings or formats.

async findOne(where: Prisma.UserWhereUniqueInput): Promise<Partial<User> | null> {
  const user = await this.prismaService.user.findUnique({ where });
  if (!user) {
    throw new NotFoundException(`User not found`);
  }
  console.log(user); // Date is correctly shown here
  return user;
}
tidal tideBOT
#

Please format your question or answer with Markdown formatting.
It leads to better readability and an easier time to spot problems.
For code blocks, you can wrap your block with three back ticks before and after the block, and after the first three back ticks you can add a language (like ts) to add syntax highlighting.
e.g.

```ts
@Injectable()
export class MySuperAwesomeService {
constructor(@Inject('InjectionToken') private readonly dep: SomeDependency) {}

getRandomNumber(): number {
return Math.round(Math.random() * 1000);
}
}
```

Becomes :point_down:

@Injectable()
export class MySuperAwesomeService {
  constructor(@Inject('InjectionToken') private readonly dep: SomeDependency) {}

  getRandomNumber(): number {
    return Math.round(Math.random() * 1000);
  }
}
weary echo
#

What is doing your serialization?

static crane
#

Thank you for your suggestions. With your help, I was able to identify and resolve the issue