fullstack-hy2020 / fullstack-hy2020.github.io

https://fullstack-hy2020.github.io
Other
1.48k stars 3.69k forks source link

update: part3a.md (GET /api/notes/:id and DELETE /api/notes/:id Endpoints Not Working) #3705

Open dev-shahed opened 1 week ago

dev-shahed commented 1 week ago

The GET /api/notes/:id and DELETE /api/notes/:id endpoints are not functioning as expected. The endpoints should return or delete the note corresponding to the given ID, but they currently do not. This issue is caused by the req.params.id being a string, while the id in the notes array is a number. As a result, the find and findIndex methods fail to match the note.

Steps to Reproduce: Start the server using node server.js. Navigate to http://localhost:3000/api/notes/1 or use a tool like curl or Postman to make a GET request. Observe that the endpoint does not return the expected note. Make a DELETE request to http://localhost:3000/api/notes/1. Observe that the note is not deleted.

Solution: Convert req.params.id to a number before using it to find or delete the note.

Ref: https://prnt.sc/ohggpLOupB_H