admin管理员组

文章数量:1221413

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

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

Share Improve this question asked Oct 31, 2018 at 15:28 bigmeisterbigmeister 1,2275 gold badges14 silver badges21 bronze badges 1
  • Is this issue fixed for you? How? Please update your answer if you have... – Cegone Commented Aug 22, 2019 at 9:53
Add a comment  | 

4 Answers 4

Reset to default 13

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.

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.

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]"

}

you must value id be number not string

{
  "id": "5", ==> "id" : 5
  "first_name": "Roger",
  "last_name": "Bacon",
  "email": "[email protected]"
},

本文标签: javascriptjsonserverstrange autoincrement idStack Overflow