I am writing nodejs rest API. I have problems with my delete function. This my html code:
<tr ng-repeat="row in displayedCollection">
<td>{{row.FirstNameAuthor}}</td>
<td>{{row.LastNameAuthor}}</td>
<td>
<button type="button" ng-click="deleteWriter(row)" class="btn btn-sm btn-danger">
</button>
</td>
</tr>
This my core.js code:
$scope.deleteWriter = function (row) {
$http.delete('/writer', $scope.row)
.error(function (data) {
console.log('Error: ' + data);
});
};
This my server.js code:
app.delete('/writer', function (req, res) {
var reqBody = '';
req.on("data", function (data) {
reqBody += data;
if (reqBody.length > 1e7) {//10MB
httpMsgs.show404(req, res);
}
});
req.on("end", function () {
wr.delete(req, res, reqBody);
});
})
Then I use function wr.delete to remove record from database.
exports.delete = function (req, resp, reqBody) {
try {
if (!reqBody) throw new Error("Input is nod valid");
var data = JSON.parse(reqBody);
if (data) {
if (!data.idRegisterIssueContract) throw new Error("No such number");
var sql = "DELETE FROM registerissuecontract ";
sql += " WHERE idRegisterIssueContract =" + data.idRegisterIssueContract;
db.executeSQL(sql, function (data, err) {
if (err) {
httpMsgs.show500(req, resp, err);
}
else {
httpMsgs.send200(req, resp);
}
});
}
else {
throw new Error("Input is nod valid");
}
}
catch (ex) {
httpMsgs.show500(req, resp, ex);
}
};
But it does not work because reqBody is empty.
How can I fixed it?
I am writing nodejs rest API. I have problems with my delete function. This my html code:
<tr ng-repeat="row in displayedCollection">
<td>{{row.FirstNameAuthor}}</td>
<td>{{row.LastNameAuthor}}</td>
<td>
<button type="button" ng-click="deleteWriter(row)" class="btn btn-sm btn-danger">
</button>
</td>
</tr>
This my core.js code:
$scope.deleteWriter = function (row) {
$http.delete('/writer', $scope.row)
.error(function (data) {
console.log('Error: ' + data);
});
};
This my server.js code:
app.delete('/writer', function (req, res) {
var reqBody = '';
req.on("data", function (data) {
reqBody += data;
if (reqBody.length > 1e7) {//10MB
httpMsgs.show404(req, res);
}
});
req.on("end", function () {
wr.delete(req, res, reqBody);
});
})
Then I use function wr.delete to remove record from database.
exports.delete = function (req, resp, reqBody) {
try {
if (!reqBody) throw new Error("Input is nod valid");
var data = JSON.parse(reqBody);
if (data) {
if (!data.idRegisterIssueContract) throw new Error("No such number");
var sql = "DELETE FROM registerissuecontract ";
sql += " WHERE idRegisterIssueContract =" + data.idRegisterIssueContract;
db.executeSQL(sql, function (data, err) {
if (err) {
httpMsgs.show500(req, resp, err);
}
else {
httpMsgs.send200(req, resp);
}
});
}
else {
throw new Error("Input is nod valid");
}
}
catch (ex) {
httpMsgs.show500(req, resp, ex);
}
};
But it does not work because reqBody is empty.
How can I fixed it?
Share Improve this question edited Jun 12, 2016 at 14:44 DzouSi asked Jun 12, 2016 at 13:49 DzouSiDzouSi 3613 gold badges7 silver badges21 bronze badges 6- What error? where is wr defined? – Thalaivar Commented Jun 12, 2016 at 14:00
- Are you trying to delete a record from a database? Have you considered using sequelize? – Roysh Commented Jun 12, 2016 at 14:00
- Yes, I am trying to delete a record from a database. But I think that I did something wrong in core.js. – DzouSi Commented Jun 12, 2016 at 14:37
- One problem here is that the DELETE method should only use query parameters, not a body. I also question the need to send the whole row across. Usually deletes just take the ID of the object. – mdickin Commented Jun 12, 2016 at 14:52
-
Your approach doesn't make sense.
DELETE /writer
means "Delete the resource at the URL /writer". There shouldn't be a request body. – Quentin Commented Jun 12, 2016 at 14:53
1 Answer
Reset to default 2You can pass an id in your delete route, like below :
Client side :
$http.delete('/writer/' + id)
.success(function(data) {
console.log('Success: ' + data);
})
.error(function(data) {
console.log('Error: ' + data);
});
Server side :
app.delete('/writer/:id', function (req, res) {
var id = req.params.id;
//DELETE YOUR RECORD WITH YOUR PARAM.
return res.status(200);
}
Tell me if I've answered your question.