json-server icon indicating copy to clipboard operation
json-server copied to clipboard

filter does not work

Open isatriya opened this issue 9 years ago • 6 comments

Hi,

i have a difficulty to use filter. Basically i have mocked-backend.js that the codes look like this: `"use strict";

const MOCK_DB_NAME = "mocked-db.json"; const PORT = 3001;

let jsonServer = require('json-server'); let bodyParser = require('body-parser'); let server = jsonServer.create(); let router = jsonServer.router(MOCK_DB_NAME); let middlewares = jsonServer.defaults();

// Set default middlewares (logger, static, cors and no-cache) server.use(middlewares); server.use(bodyParser.json());

// require('./mocked-api-routes')(server);

// Use default router server.use(router) server.listen(PORT, () => { console.log(JSON Server is running on port ${PORT}); })`

and the mocked-db.json as follows :
{ "test-steps": [ { "test-suite-id": "1", "test-case-id": "1", "step-id": "71effb21-05f0-437c-b028-cb9a7648d3f1", "test-step-sequence": "1", "action": "Dial number", "input": "123", "wait": "1", "notes": "asdf" }, { "test-suite-id": "1", "test-case-id": "1", "step-id": "71effb21-05f0-437c-b028-cb9a7648d3f2", "test-step-sequence": "2", "action": "Dial number", "input": "123", "wait": "1", "notes": "asdf" } ] }

i try to filter the mocked-db.json using GET http://localhost:3001/test-steps?test-case-id=1 and it does not work. am i missing something?

thank you.

isatriya avatar Jun 09 '16 05:06 isatriya

I am having exactly the same problem when trying to filter on a field which is a number

REBELinBLUE avatar Jul 04 '16 13:07 REBELinBLUE

JSON Server automatically convert query parameters to their native type. For example, propA=true&propB=1 will be converted internally to 1 and true (instead of "1" and "true")

This way, in your db.json you can write:

{
  "propA": 1,
  "propB": true
}

Instead of:

{
  "propA": "1",
  "propB": "true"
}

To fix your issue, I would suggest updating db.json and replace strings with integers when possible.

typicode avatar Jul 04 '16 15:07 typicode

@typicode when i am trying to modify the as you stated above, while starting json server i am getting error

vickyru avatar Jan 30 '19 10:01 vickyru

Have the same problem. But in my case it doesn't work with string values either.

db.json tickets: { "0": { "ID": "INC012345103", "AssignedUser": null, "Caption": "High CPU", "Description": null, "Priority": "1", "Project": "Project1", "Status": null, "Team": "blah blah blah", "LastCallLevel": "0", "CallStatus": "Not required", "LastCallStatus": "4", "LastUpdatedTime": "2019-09-19 18:48:43", "CreatedTime": "2019-09-18 18:16:43" }, "1": { "ID": "INC012345104", "AssignedUser": null, "Caption": "High CPU", "Description": null, "Priority": "1", "Project": "Project2", "Status": null, "Team": "blah blah blah", "LastCallLevel": "0", "CallStatus": "Not required", "LastCallStatus": "4", "LastUpdatedTime": "2019-09-19 18:48:43", "CreatedTime": "2019-09-15 18:16:43" } }

when I'm trying "tickets?ID=INC012345103" it just returns the whole data, ignoring the condition.

any clue?

Thanks in advance

lukaskane avatar Oct 27 '19 16:10 lukaskane

Any update on this ?

DavidWz0403 avatar Sep 15 '22 12:09 DavidWz0403

I have the same issue however it works for some endpoints and for some others not

DavidWz0403 avatar Sep 15 '22 12:09 DavidWz0403