Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json-server - strange autoincrement id

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.?

like image 575
bigmeister Avatar asked Aug 25 '26 11:08

bigmeister


1 Answers

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.

like image 98
bonniss Avatar answered Aug 28 '26 00:08

bonniss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!