typicode / json-server

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

Can't filter on a specific field #219

Open romainPrignon opened 8 years ago

romainPrignon commented 8 years ago

Hi,

I wonder if i'm doing something wrong It's seems that I can't filter on a specific field

When I go to http://localhost:4000/vehicles?date=201207

I got

[]

But when I go to http://localhost:4000/vehicles?brand=RENAULT

I got

[
    {
        label: "renault megane"
        brand: "RENAULT",
        model: "MEGANE III",
        date: "201207"
    }
]

this is my fixtures.json file:

"vehicles": [
    {
      "label": "renault megane",
      "brand": "RENAULT",
      "model": "MEGANE III",
      "date": "201207"
    }
  ]

and I launch the server with : json-server -w -q -p 4000 -d 100 fixtures.json

gagan-bansal commented 8 years ago

The date field is string in your db. In request params its being parsed as number. If the db is like:

{
"vehicles": [
    {
      "label": "renault megane",
      "brand": "RENAULT",
      "model": "MEGANE III",
      "date": 201207
    }
  ]
}

then the request is working fine

http://localhost:4000/vehicles?date=201207

But there must be some solution to keep numbers as string in db.

romainPrignon commented 8 years ago

your are right. I tried "http://localhost:4000/vehicles?date=201207" with :

{
"vehicles": [
    {
      "label": "renault megane",
      "brand": "RENAULT",
      "model": "MEGANE III",
      "date": 201207
    }
  ]
}

and it worked. But :

 "date": "201207"

do not work. I'm going to check why this is jappening

Thanks for your solution !

typicode commented 8 years ago

@gagan-bansal thank you for the explanation

@romainPrignon json-server automatically tries to convert query parameters, so if you have ?some_boolean=true&some_integer=1 they will be converted: "true" -> true and "1" -> 1. This allows to use native types other than strings in databases. If it can't convert a string to an integer it will keep the string though.

gagan-bansal commented 8 years ago

@typicode thanks for detailed explanation. How would this case be resolved many times ids are number but stored as string like `001'?

bugss commented 8 years ago
 return _.matchesProperty(key, value)(element)||_.matchesProperty(key,originValue)(element);

just change the code in the plural.js . i solve my problem ..

ladaniavadh commented 4 years ago

Hi,

I wonder if i'm doing something wrong It's seems that I can't filter on a specific field

When I go to http://localhost:4000/vehicles?date=24/08/2008

I got the data in response but when

http://localhost:4000/vehicles?date_gte=23/08/2008&date_lte=25/08/2008 i got []

my object is like { "athlete": "Natalie Coughlin", "age": 25, "country": "United States", "year": 2008, "date": "24/08/2008", "sport": "Swimming", "gold": 1, "silver": 2, "bronze": 3, "total": 6 }

gagan-bansal commented 4 years ago

Just try storing date as 'YYYY/DD/MM', though I have not tested but in general this is the way date is store as string for sorting purpose.