Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is not possible to filter based on collection property. #1449

Open
hamidmayeli opened this issue Sep 13, 2023 · 2 comments
Open

It is not possible to filter based on collection property. #1449

hamidmayeli opened this issue Sep 13, 2023 · 2 comments

Comments

@hamidmayeli
Copy link

Given the following data:

[
    {
        "id": 2,
        "rate": 5,
        "logo": "https://loremflickr.com/640/480?lock=6423556804050944",
        "email": "klalh35@gmail.com",
        "telephone": "021 178 03 73",
        "categories": [
            {
                "id": 1,
                "name": "cat 1"
            },
            {
                "id": 2,
                "name": "cat 2"
            }
        ]
    }
]

Is it possible to filter based and category having a specific id.

I have tried http://localhost:4000/business?categories.id=10 and it didn't work.

@notadevps
Copy link

First, only objects are supported and second, if you want to filter based and category having a specific ID.
then you can create categories as separate objects
and link them to business objects like
{ "business": [ { "id": 2, "rate": 5, "logo": "https://loremflickr.com/640/480?lock=6423556804050944", "email": "klalh35@gmail.com", "telephone": "021 178 03 73" } ], "categories": [ { "id": 1, "name": "cat 1", "businessId": 1 }, { "id": 2, "name": "cat 2", "businessId": 2 } ] }
now you can get categories
http://localhost:3000/categories?id=2
and you can also get specific business object
http://localhost:3000/categories?businessId=1

@MadhuSaini22
Copy link

Given the following data:

[
    {
        "id": 2,
        "rate": 5,
        "logo": "https://loremflickr.com/640/480?lock=6423556804050944",
        "email": "klalh35@gmail.com",
        "telephone": "021 178 03 73",
        "categories": [
            {
                "id": 1,
                "name": "cat 1"
            },
            {
                "id": 2,
                "name": "cat 2"
            }
        ]
    }
]

Is it possible to filter based and category having a specific id.

I have tried http://localhost:4000/business?categories.id=10 and it didn't work.

The reason your query http://localhost:4000/business?categories.id=10 didn't work is that json-server doesn't support filtering by properties of nested objects or arrays, like the categories array in your data, using its default query parameters. It only supports filtering on top-level fields directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants