Reputation: 1227
I've got a problem with id-autoincrementation on my json-server. I set up a simple default json-server with single db.json file to watch.
Here is my db.json file (simple generated data):
{
"users": [
{
"id": 2,
"first_name": "Lucy",
"last_name": "Ballmer",
"email": "[email protected]"
},
{
"id": 3,
"first_name": "Anna",
"last_name": "Smith",
"email": "[email protected]"
},
{
"id": 4,
"first_name": "Robert",
"last_name": "Brown",
"email": "[email protected]"
},
{
"id": "5",
"first_name": "Roger",
"last_name": "Bacon",
"email": "[email protected]"
},
]
}
Now I'm trying to POST some data to db.json using Postman. I have configured endpoint:
http://localhost:4040/users
And I'm doing POST with following data:
{
"first_name": "Marian",
"last_name": "Gowno3333",
"email": "[email protected]"
}
Json-server increments IDs automatically by default, but as a result I get strange id format like so:
"id": "8OZrQkH"
Instead of getting for example "id": 6.
Why is this happening? Is there any option to set IDs format as 1, 2, 3 etc.?
Upvotes: 6
Views: 12857
Reputation: 11
Even I'm seeing the same issue. But I have not used string in ID
{
"users": [
{
"id": 1,
"name": "Sudhanshu",
"age": 30,
"email": "[email protected]"
},
{
"id": 2,
"name": "Varun",
"age": 26,
"email": "[email protected]"
}
]
}
Body-
{
"name": "abc",
"age": 20,
"email": "[email protected]"
}
Output-
{
"id": "51fd",
"name": "abc",
"age": 20,
"email": "[email protected]"
}
Upvotes: 1
Reputation: 71
I had the same problem but even when I turn the id to a number, it still generates a random string id. Then I have found that in another application, this problem didn't happen. The difference between them: the json-server version. So after I downgraded from "1.0.0-alpha.23" to "^0.17.4", the problem was solved.
Upvotes: 1
Reputation: 1
you must value id be number not string
{
"id": "5", ==> "id" : 5
"first_name": "Roger",
"last_name": "Bacon",
"email": "[email protected]"
},
Upvotes: 0
Reputation: 505
The last record's id type is string
: "5"
, so the server generate a random string for next id. Just make it number to fix.
Upvotes: 13