I'm trying to flatten a nested object in my Loopback and Typescript controller
Here's the structure of my model :
export class SampleModel {
id: number;
code: number;
guide?: string;
gradeData?: string;
}
Take a look at this example object :
{
"id": 1,
"code": 12345,
"guide": "Guide for 2021",
"gradeData": {
"en": "Eng grade",
"de": "Ger grade"
}
}
This is how my controller function looks like:
// returns an array of SampleModel objects
@get('/guides')
async find(
@param.query.string('lang') lang: string,
@param.filter(SampleModel) filter?: Filter<SampleModel>
): Promise<SampleModel[]> {
return this.sampleModelRepository.find(filter); //this returns Promise<SampleModel[]>
}
I aim to modify the response based on the lang
parameter. For instance, if lang = en
, I want the output to be like:
[
{
"id": 1,
"code": 12345,
"guide": "Guide for 2021",
"gradeData": "Eng grade"
}
]