Search code examples
typescriptexpressswaggerswagger-uitypeorm

Response body comes with correct array of objects count but objects are empty


I just started a Ts.ED project using TypeORM and Swagger to connect to my database. I have a UserController.ts file and a UserService.ts file. In the service file, I have an async function in which I retrieve all the users in my db

async find(): Promise<Users[]> {
  const users = await this.connection.manager.find(Users);
  return users
}

Then in my controller file I call that service function, find, for its response to show up on swagger ui

@Get('/')
@Returns(200, Array).Of(Users)
async findAll(): Promise<Users[]> {
  const users = await this.usersService.find()
  console.log('user',users)
  return users
}

The thing is that the console.log call does print everything as it should, but on swagger ui, the response body only shows

[
  {},
  {},
  {}...
]

although with the correct count of objects, but empty.

Now, if I change the controller function to

@Get('/')
//@Returns(200, Array).Of(Users)
async findAll(): Promise<Users> {
  const users = await this.usersService.find()
  console.log('user',users)
  return users[0]
}

the object shows up correctly in swagger ui, with all of its properties.

Any idea why with the array, its objects are showing up empty?


Solution

  • Alright, I figured it out. So, I misunderstood part of TypeORM's and Ts.Ed's documentation. In TypeORM's, the database models are called Entities. I had a file a User.entity.ts and a User.model.ts file, mostly identical so that seemed a bit off at the beginning. Now, by looking at @some-user's answer, as replied, I added the @Property decorator in the model file. Then I tried putting it the entity file and the objects in the array were no longer empty, they were showing the properties in which I put the @Property decorator.