【问题标题】:AngularJS/node.js/mongoDB. Trying to delete a row from a tableAngularJS/node.js/mongoDB。试图从表中删除一行
【发布时间】:2017-01-08 11:01:37
【问题描述】:

有人知道为什么会出错吗?

        $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 语句。

【问题讨论】:

  • 为什么不用statusCodemessage 之类的其他东西来满足您的条件

标签: javascript angularjs node.js mongodb


【解决方案1】:

这不是您在 Angular 的 http API 中处理错误的方式,也不是一般的 Promise。为什么要测试一个名为 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();
        }})

};

【讨论】:

  • "你为什么要测试一个叫做 status 的值?"我对此很陌生,我正在查看其他一些代码,我认为它是一个内置函数。感谢您为我解决这个问题。
【解决方案2】:

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

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

};

【讨论】:

    猜你喜欢
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多