angular / in-memory-web-api

The code for this project has moved to the angular/angular repo. This repo is now archived.
MIT License
1.18k stars 232 forks source link

InMemoryDbService can not add new item when collection is empty #181

Closed soleanos closed 6 years ago

soleanos commented 6 years ago

Hi, I'v an issue about adding new items to an empty collection : i get the error : "Collection 'stories' id type is non-numeric or unknown. Can only generate numeric ids."}"

status:422 statusText:"Unprocessable Entity" url:"api/stories"

This work fine when i'v one item or more but after deleting all items of the collection this error happen.. any ideas ?

Thanks

wardbell commented 6 years ago

The problem is that you are adding an item without an id. All entities must have an id. When the library sees that the entity-to-add lacks an id, it assumes that you want the server to generate one for you. It will try if it can determine the type of the id from other entities in the collection. But because there aren't any, it gives up.

In future we may have some metadata to guide it. Right now the solution is to provide a custom genId() function as explained in the README.

Closing because that's the prescribed solution for now.

Dok11 commented 6 years ago

@wardbell what if I want send POST FormData. That doesnt have id