json-server
json-server copied to clipboard
How to delete multiple items at once?
There's a list contain some items like this
{
card: [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' }
]
}
I want to delete the first item and the second item.
I try this,but do‘t work
http.delete("path/card/1,2")
thx
I think you can do like that because your path is wrong. Try to delete path by path and push to all Promises to one array and after all use Promise.all. Still, you must be careful and create a handler for a moment when some command will be ended with an error.
@michalstol Promise.all
is a solution, But I want to delete multiple items in only one request.
In my scene,I have to limit the number of requests.
You can try in this way. Delete all card and send in post new object without specific records.
@michalstol finally, I fork this repo and make it support delete multiple items in a request,like this
http.delete("path/id1,id2")
https://github.com/Moon-Land/json-server
@typicode Would you add this feature?
Nice :)
@typicode Would you add this feature?
Oh, thanks
I wonder why there's still no such a basic feature to delete all records. Why do we have to remove all of the items in the array making a call for each item instead of a single one? That's insane.
@Gavin-Gong @michalstol hey guys, you said that Promise.all
is a solution, but in my case the server crashes when I try to use it and I can't figure out why.
I'd appreciate it if you check this issue #1037
For now, I modify these lines of codes to get multi-id-delete feature, open file plural.js, change from
resource = db.get(name).removeById(req.params.id).value(); // Remove dependents documents
const removable = db._.getRemovable(db.getState(), opts);
removable.forEach(item => {
db.get(item.name).removeById(item.id).value();
});
to
req.params.id.split(',').filter(id => id !== '' || id !== undefined || id !== null).forEach(id => {
resource = db.get(name).removeById(id).value(); // Remove dependents documents
const removable = db._.getRemovable(db.getState(), opts);
removable.forEach(item => {
db.get(item.name).removeById(item.id).value();
});
});
```
We usually install json-server global, for example with yarn, the code will be located at
`%LocalAppData%\Yarn\Data\global\node_modules\json-server\lib\server\router`
@lloydbanks Regarding server crashes, yeah, it definitely happens with multiple 'deletes.' NS what threshold is maybe more than 3 per second?
Seems like this is a very simple feature to implement. Are there any intentions to implement it in the near future?
Is there anything new about this functionality in 2021?
I also had this problem, and solved it by removing the watch flag when starting json-server. This way the server doesn't reload automatically and all the requests are successfully handled.
this is what I did!
const headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' }
let delArray = [1,3,4,5] let delFetch = delArray.map(eleid => { return fetch(config.url + '/dataposts/' + eleid, { method: 'DELETE', headers: headers, }); });
const res = await Promise.all([delFetch]);
It works like deleteMany (Not actually)
https://github.com/nicolasbarb/json-server/commit/d4b3a0e58048ad53673f4d283d866c7c69f9da9f#comments
Hello !, I have equal problems same you but If I test this line : //Delete all product in order deleteAllProductInOrder(){ return this.http.delete("http://localhost:3000/orders"); }
I have this bug that display in my console : payement.component.ts:55
DELETE http://localhost:3000/orders 404 (Not Found) ERROR HttpErrorResponse {headers: HttpHeaders, status: 404, statusText: 'Not Found', url: 'http://localhost:3000/orders', ok: false, …}
I'm having the same issue now, have no idea when this feature would be added
i found a way this could work
- you fetch the data you want to delete then map....since all items in the array of data has a unique id, then you map through those data calling the delete endpoint individually .... i.e that clears all the items one by one
export const getSessions = async () => { const response = await axios.get("http://localhost:3001/sessions"); return formatSuccessResponse(response); }; const { data: s } = getSessions();
export const deleteSession = async (id: string) => {
const response = await axios.delete(http://localhost:3001/sessions/${id}
);
return response;
};
<Button onClick={() => { s.map(({ id }) => { deleteSession(id as string); return; }); } />