How to show populated nested array result in loopback4?

28 views
Skip to first unread message

Noufal

unread,
Feb 17, 2021, 12:13:16 AM2/17/21
to LoopbackJS

I wanted to store categories and its products Ids as an array in the collection. On get method I need show category info as well as product details as mentioned below .Is there any best with relational model in loopback? I am new in Loopback and node js, please suggest best practice.

Category
{
    "id": "1",
    "Category": "Category 1",
    "products": [
      "1",
      "2"
    ]
},
{
    "id": "2",
    "Category": "Category 2",
    "products": [
      "2",
      "5"
    ]
},
{
    "id": "3",
    "Category": "Category 3",
    "products": [
      "3",
      "4"
    ]
}

Products
{
    "id": "1",
    "title": "Product 1"
},
{
    "id": "2",
    "title": "Product 2"
},
{
    "id": "3",
    "title": "Product 3"
},
{
    "id": "4",
    "title": "Product 4"
},
{
    "id": "5",
    "title": "Product 5"
}

Result 
{
    "Category": "Category 1",
    "products": [
      {
   "id": "1",
       "title": "Product 1"
},
{
   "id": "2",
       "title": "Product 2"
}
    ]
},
{
    "Category": "Category 2",
    "products": [
      {
   "id": "2",
       "title": "Product 2"
},
{
   "id": "5",
       "title": "Product 5"
}
    ]
},
{
    "Category": "Category 3",
    "products": [
      {
   "id": "3",
       "title": "Product 3"
},
{
   "id": "4",
           "title": "Product 4"
}
    ]
}
Reply all
Reply to author
Forward
0 new messages