Im running in to some hardships trying to figure out how to return selected fields from nested joins.
So in my entities i have a "Restaurant" witch have 1:M relationship with "Product". "Products" have a M:M relationship with "Category. And "Category" have a M:M with "Menu".
Right now im using this snippet to return all the products with the categories and menus populated:
const place = await this.placeRepo.findOne(placeID, {
populate: ['products.categories.menus'],
});
And the problem is that it returns all the fields of products, category & menu which is very unnessecary. Only uuid & title is neeccessary basically. So i want to return the data like this:
[
{
"uuid": "tCj6soMp6YWcJosTxvjJ1",
"title": "Burger 2",
"categories": [
{
"uuid": "JUwta7kMG1-DUARA3Tnan",
"title": "Pizza",
"menus": [
{
"uuid": "tohzpXtkAN-mW646sphE1",
"titleOfMenu": "Lunch",
}
]
}
]
},
]
I have tried this snippet for example just to determine the fields of the return for the products but it does not work and i dont know how to get around it:
const place = await this.placeRepo.findOne(placeID, {
populate: ['products.categories.menus'],
fields: ['products', 'products.title']
});
which throws the following error: "TypeError: Cannot read property '0' of undefined"
How is it possible to return selected fields from nested populated entities? Do we have to use a querybuilder instead or am i missing some simple stuff here?
Updating to version 5.0.2 and changing the code to this did the work! Actually dont even need to specify the "populate" as mikro orm populates them automatically for you.
const place = await this.placeRepo.findOne(placeID, {
fields: [
'products',
'products.title',
'products.categories.title',
'products.categories.menus',
'products.categories.menus.titleOfMenu',
],
});