typicode / json-server

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

Random id values. #1477

Closed InsipidLake closed 10 months ago

InsipidLake commented 10 months ago
{
  "users": [
    {
      "id": 1,
      "name": "John Doe",
      "username": "john.doe",
      "email": "john.doe@example.com",
      "phone": "123-456-7890"
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "username": "jane.doe",
      "email": "jane.doe@example.com",
      "phone": "987-654-3210"
    }
  ]
}

Now if i insert into the API using postman or any other way. The next id comes out randomly "de302" or "7621" or something along those lines. image

textbook commented 10 months ago

As oposed to what? That's what's supposed to happen, a random (four character hex) ID gets generated on insertion:

https://github.com/typicode/json-server/blob/e3ce04a04a2777b133a8efa27c59e29121bf24a4/src/service.ts#L108-L110

If you were expecting numerical IDs incrementing from the values you've supplied, that's in the stable v0 but not (currently) the alpha v1:

https://github.com/typicode/json-server/blob/06bdf112aa79ea7a905f927435609ec20a873845/src/server/mixins.js#L50-L51

typicode commented 10 months ago

It's normal behavior. JSON Server v1 expects IDs to be strings, and it returns random ones upon the creation of a new resource.