Closed ugocottin closed 3 years ago
@Jawtoch The field
API can only be used to control what goes into the model, not how it is encoded. It can be used to optimize your database queries by not loading unnecessary data.
What you're observing is that the fields that are not given any value are being accessed during encoding which causes the fatal error.
You'll have to use a separate Encodable value to determine what gets output.
With: Vapor 4.41.4, Fluent 1.11.0
I wonder how to only expose certains field to the user after a db request, without creating a DTO struct
I have a
Employee
class:and the
WorkGroup
class:Currently, I fetch the DB with
A request will give the output:
But I want, for example, the following output:
Is that possible with the
.field(_: )
API ? I tried with:As a result, I get a fatal error
Cannot access field before it is initialized or fetched: (a field of the Employee class)
.I am using the
.field(_: )
right? It that possible?