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 231 forks source link

cannot create new rows when the InMemoryDbService DB is empty: id issue #206

Open Fanchao92 opened 6 years ago

Fanchao92 commented 6 years ago

In my dev environment, I use InMemoryDbService to emulate a back-end database. Now I am trying to create a new row in my DB when the DB is empty. I send a post request with my new object, except that the object does NOT have an id and I am assuming the server will auto-create it for me. However, the post request fails due to 'id type is non-numeric or unknown. Can only generate numeric ids.'

lk77 commented 6 years ago

Hello, you can add a genId method to your service to handle the ids, someting like that :

genId<T extends { id: any }>(collection: T[], collectionName: string): any {
        return collection.reduce((prev, curr) =>
        {
            return (curr.id + 1) || 0;
        }, 1);
    }