nomanHasan / todo-api

A simple todo API made with NodeJS, ExpressJS and MongoDB. Can be used as a back-end for learning front-end technologies without making a standalone API for that purpose.
26 stars 22 forks source link

some schema missing in CRUD operations #2

Open staminna opened 6 years ago

staminna commented 6 years ago

Hi

The description and date fields are missing in Creating (checked using postman).

screen shot 2017-12-16 at 15 11 19

Deleting crashes:

Not Found

404

Error: Not Found
at /Users/jorge/webapp/angular.io/todo-api/app.js:50:13
at Layer.handle [as handle_request] (/Users/jorge/webapp/angular.io/todo-api/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/jorge/webapp/angular.io/todo-api/node_modules/express/lib/router/index.js:317:13)
at /Users/jorge/webapp/angular.io/todo-api/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/jorge/webapp/angular.io/todo-api/node_modules/express/lib/router/index.js:335:12)
at Immediate.next (/Users/jorge/webapp/angular.io/todo-api/node_modules/express/lib/router/index.js:275:10)
at Immediate.<anonymous> (/Users/jorge/webapp/angular.io/todo-api/node_modules/express/lib/router/index.js:635:15)
at runCallback (timers.js:802:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)
nomanHasan commented 6 years ago

@staminna You need to fill the x-www-form-urlencoded POST Body fields to make a successful POST request. todo-api-post-issue

staminna commented 6 years ago

Thanks for quick reply @nomanHasan I can post but not delete. See above error stack

williamJmelton commented 6 years ago

I am also having an issue with delete. I don’t understand how it works: the url has an :id in it in the route file, how do I pass this into the api? Doing /api/todos/[key] returns an error but it deletes the item. If I pass the id in the body of postman req than I get an 404...why? How does the server know the key I pass is the id if I don’t specify it in the req body?

williamJmelton commented 6 years ago

I figured out the error on delete. The delete works but the success code was wrong: return res.status(204).json({status:204, message: "Succesfully Deleted Item"})

should be:

return res.status(200).json({status:200, message: "Succesfully Deleted Item"})

nomanHasan commented 6 years ago

@williamJmelton Thank your very much for sharing your findings. But according to the HTTP Method definitions a status code of 204 No Content is acceptable as a successful DELETE response. See this -

https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

(Section 9.7 DELETE) A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity.

The {status, message} object is not mandatory. The main resource is the Todo object. As we are not returning any Todo object on DELETE, a 204 No Content makes more sense. But 200 OK should be fine too.

hjunaidshahid commented 6 years ago

@williamJmelton thanks to share your findings but after that change i did face same issue.

hjunaidshahid commented 6 years ago

I fixed issue on delete. The delete works with success code: if(deleted.result.n === 0){ should be: if(deleted.n === 0){

ackoujens commented 5 years ago

@hjunaidshahid Thanks man! I have no clue why this works but glad I found your comment! Should've been more upvoted!