1

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?

6
  • What error? where is wr defined? Commented Jun 12, 2016 at 14:00
  • Are you trying to delete a record from a database? Have you considered using sequelize? 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. 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. 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. Commented Jun 12, 2016 at 14:53

1 Answer 1

2

You 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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have added this to my code but my id is hot defined in app.delete
I've updated my post, it should be work correctly now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.