typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.89k stars 7.03k forks source link

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

Open hamidmayeli opened 1 year ago

hamidmayeli commented 1 year ago

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.

sahil-arora-76 commented 11 months ago

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 commented 10 months ago

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.