Skip to content

How to do referencesmany in loopback 4 to populate relational model data ? #7102

@noufalnoufu

Description

@noufalnoufu

Can you explain how to do referencesmany in loopback 4

https://loopback.io/doc/en/lb3/Embedded-models-and-relations.html#referencesmany

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",
"3"
]
}

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

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": "3",
"title": "Product 5"
}
]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions