0

Anyone got any idea why this goes wrong?

        $scope.removeProduct = function(product){

    console.log(product._id);
    $http.delete("/api/products/" + product._id)
                .success(function (data) {
                    if (data.status == 1) {
                            console.log("got here");

    var index = $scope.vehicles.indexOf(product);
    $scope.vehicles.splice(index, 1);

                    } else {
                            console.log("Something went wrong", product._id);
                            console.debug();
                    }})

    };

This always goes to the else statement.

1
  • why not use something else for your condition like the statusCode or message Commented Jan 8, 2017 at 2:31

2 Answers 2

0

That's not how you handle errors in Angular's http API, nor with promises in general. Why are you testing for a value called status?

$scope.removeProduct = function(product){

    $http.delete("/api/products/" + product._id)
        .then(function (response) {
            var index = _.indexOf($scope.data, product);
            var index = $scope.vehicles.indexOf(product);
            $scope.vehicles.splice(index, 1);
        }).catch(function (response) {
            console.log("Something went wrong", product._id, response);
            console.debug();
        }})

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

1 Comment

"Why are you testing for a value called status?" I'm very new at this, I was looking at some other code and I thought it was a built in function. Thanks for clearing that up for me.
0

Better check for "data" variable whether it has "status" or not

$scope.removeProduct = function(product) {
  console.log(product._id);
  $http.delete("/api/products/" + product._id)
    .success(function(data) {
      if (data && data.status == 1) {
        console.log("got here");
        var index = _.indexOf($scope.data, product);
        var index = $scope.vehicles.indexOf(product);
        $scope.vehicles.splice(index, 1);
      } else {
        console.log("Something went wrong", product._id);
        console.debug();
      }
    });

};

Comments

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.