Skip to content

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

Closed

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

Activity

  1. JerryHuang2015 commented on Feb 19, 2021

    @JerryHuang2015
  2. noufalnoufu commented on Feb 20, 2021

    @noufalnoufu
    Author

    Can you provide and example, I had spend almost three weeks in your documentation and couldn't able to figure it out, you could have include this kind of one sample in you shopping cart examples. Is it possible to use hasMany without using belongsTo in the target model

  3. JerryHuang2015 commented on Feb 21, 2021

    @JerryHuang2015

    it is very clear on loopback 4,
    @hasMany(() => Order) orders?: Order[];
    1st check your Loopback version
    one to many relation https://loopback.io/doc/en/lb4/HasMany-relation.html
    many to many relations https://loopback.io/doc/en/lb4/HasManyThrough-relation.html

  4. noufalnoufu commented on Feb 21, 2021

    @noufalnoufu
    Author

    In your example source model’s id as the foreign key property (customerId) in the target model. But in my case I don't have any relation with category white creating the product. When I create a category I will be adding products Ids as array in to it. When I access category I need to populate product detail as well. Is there any way to do it without foreign key property in the target model?

  5. noufalnoufu commented on Feb 23, 2021

    @noufalnoufu
    Author

    {
    "@loopback/authentication": "^4.2.5",
    "@loopback/authentication-jwt": "^0.3.1",
    "@loopback/boot": "^2.3.1",
    "@loopback/context": "^3.8.1",
    "@loopback/core": "^2.7.0",
    "@loopback/openapi-v3": "^3.4.1",
    "@loopback/repository": "^2.5.1",
    "@loopback/rest": "^5.0.1",
    "@loopback/rest-explorer": "^2.2.2",
    "@loopback/service-proxy": "^2.3.0",
    "@types/bcryptjs": "^2.4.2",
    "@types/lodash": "^4.14.152",
    "bcryptjs": "^2.4.3",
    "isemail": "^3.2.0",
    "jsonwebtoken": "^8.5.1",
    "loopback-connector-mongodb": "^5.5.0",
    "loopback-connector-mysql": "^5.4.3",
    "tslib": "^1.10.0"
    }

    This is my application version. I have found solution for my issue in lb3
    https://loopback.io/doc/en/lb3/Embedded-models-and-relations.html#referencesmany
    I am looking for same thing in lb4. Please help me, my entire application development got stuck here, other wise I have to switch to core node and express.

  6. locked and limited conversation to collaborators on Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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