AngularJS / node.js中/ MongoDB的。试图从表中删除一行

时间:2017-01-08 02:19:53

标签: javascript angularjs node.js mongodb

任何人都知道为什么会出错?

        $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();
                    }})

    };

这总是转到else语句。

2 个答案:

答案 0 :(得分:0)

这不是你如何处理Angular的http API中的错误,也不是一般的承诺。为什么要测试一个名为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();
        }})

};

答案 1 :(得分:0)

更好地检查“数据”变量是否具有“状态”

$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();
      }
    });

};