dchester / epilogue

Create flexible REST endpoints and controllers from Sequelize models in your Express app
846 stars 116 forks source link

resource.create generates an insert statement which misses optional fields? #115

Closed rvnath closed 8 years ago

rvnath commented 8 years ago

I used the "getting started" example given here: https://www.npmjs.com/package/epilogue App runs and works fine for retrieval, and listing. But when I try to create a resource by using curl, it generates an insert query which misses the username and birthdat fields, and hence only adds id, updatedAt and createdAt fields.

This is my curl invocation:

curl -d '{"username":"sudhakar","birthday":"1975-15-05", "updatedAt":"2015-10-10","createdAt":"2015-10-11"}' http://localhost:50420/users

and the response is: {"id":1,"username":null,"birthday":null,"createdAt":"2015-11-08T15:06:59.000Z","updatedAt":"2015-11-08T15:06:59.000Z"}

On the server console, I get the below output... rvnath@rvnath-pc ~/workpad/workouts/node/reqr $ node restapi,js listening at http://:::50420 Executing (default): INSERT INTOUsers(id,updatedAt,createdAt) VALUES (DEFAULT,'2015-11-08 15:06:59','2015-11-08 15:06:59'); Executing (default): SELECTid,username,birthday,createdAt,updatedAtFROMUsersASUserWHEREUser.id= 1;

As you can see, it insert statement is missing username and birthday fields. What am I doing wrong here?

mbroadst commented 8 years ago

@rvnath I believe this is because you aren't posting the data correctly. As you can see the values that are returned "properly" (createdAt, updatedAt) are actually automatically filled in by sequelize on every request because you haven't disabled timestamps. Perhaps you should try this instead:

curl -H "Content-Type: application/json" -X POST -d '{"username":"sudhakar","birthday":"1975-15-05", "updatedAt":"2015-10-10","createdAt":"2015-10-11"}'  http://localhost:50420/users
philotas commented 8 years ago

@rvnath to clarify a litte: If you use curl with POST Data it sends this header Content-Type: application/x-www-form-urlencoded which requires the header to look like GET parameters, but in the body: e.g.: username=sudhakar&id=2

But you are having your POST body in JSON that is why @mbroadst adds the correct header to the curl call.

rvnath commented 8 years ago

Got it. My bad.