exports.deleteCustomer = asyncHandler(async (req, res) => {
const id = req.params.id;
if (!id) {
res.status(400).send({ message: "can't remove ,invalid customer" });
return
}
try {
const data = await Customer.destroy({ where: { id: id } })
console.log(data, id, "deleteone")
res.status(200).json(data);
} catch (error) {
res.status(400);
throw new Error(error.message || "can't remove Customer");
}
})
This is how I called in postman http://localhost:5000/customerManager/customer/5
console say 1 5 deleteone
postman response is 1
how can i get that instance value
The delete code you provided is functional and can delete a customer record from the database.
Response Format: The response format for a successful
DELETEoperation is typically an acknowledgment message or an empty response body. Returning the deleted data might be confusing, as it's not a common practice for DELETE requests. Instead, you can return a simple success message.For a successful DELETE operation, the most appropriate HTTP status code to use is 204 No Content. This status code indicates that the request was successfully processed, and there is no additional content to include in the response body. It communicates that the resource has been deleted without returning any data.
While you can use a 200 OK status code with a response body to convey additional information about the deleted resource, it is not as standard or explicit as 204 No Content for DELETE operations. It may also introduce some ambiguity, as clients might expect a response body with data when a 200 status code is used.
The HTTP 204 No Content status code is defined to indicate that a request has been successfully processed, but there is no content to return in the response body. According to the HTTP specification, when a server responds with a 204 No Content status code, it should not include a message body.
You need to change your server code for deleting.
Demo code
Using SQLites and sequelize.js
Save as 'create_database.js'
Install dependencies
Create Database
Server
Save as server.js
Run server
Result
Using DB Browser for SQLite
API testing by Postman
Create customer
Body data
Get customer
Update customer
Body data
Delete customer