Search code examples
javascriptreactjsjson-server

json-server GET /posts?title_like=server not working


this is my json-object

{
  "products": [
    {
      "id": 10001,
      "name": "Basics To Advanced In React",
      "overview": "SOMETHING",
      "long_description": "SOMETHING",
      "rating": 5,
      "in_stock": true,
    },
    {
      "id": 10002,
      "name": "Django Framework for Beginners",
      "overview": "SOMETHING",
      "long_description": "SOMETHING",
      "rating": 5,
      "in_stock": false,
    },
  ],
}

other query's are working such as given below

http://localhost:8000/products [ works ]
http://localhost:8000/products?id=10001 [ works ]

but these 2 below are not working.

http://localhost:8000/products?name_like=react [ doesnt work ]
http://localhost:8000/products?in_stock=true [ doesnt work ]

this is the API call,

useEffect(() => {
    async function fetchProducts() {
      const response = await fetch(
        `http://localhost:8000/products?name_like=${searchTerm}` // if searchTerm = react
      );

      const data = await response.json();
      setProducts(data);
    }
    fetchProducts();
  }, [searchTerm]);

consol.log(documents) - logs an empty array [] where as i should be getting this object when calling http://localhost:8000/products?name_like=react

{
  "id": 10001,
  "name": "Basics To Advanced In React",
  "overview": "SOMETHING",
  "long_description": "SOMETHING",
  "rating": 5,
  "in_stock": true,
},

Solution

  • The latest versions of json-server doesn't have "_like".

    You might have to uninstall json-server with

    npm uninstall -g json-server
    

    Then install the 0.17.4 version

    npm i -g [email protected]