【问题标题】:to delete data in mongodb using angularjs and node.js使用 angularjs 和 node.js 删除 mongodb 中的数据
【发布时间】:2018-01-12 19:02:12
【问题描述】:

我想使用 angularjs 和 node.js 删除 mongodb 中的数据,但无法删除 /api/manage-product 错误出现在控制台中。

.html 文件

<tbody>
                <tr ng-repeat="product in vm.result">
                    <td>{{ product.Product_Name }}</td>
                    <td>{{ product.Brand }}</td>
                    <td>{{ product.Color }}</td>
                    <td>{{ product.Price }}</td>
                    <td>{{ product.Rating }}</td>
                    <td><img style="heigth:30px; width:30px;" src='{{ product.Image }}'></img></td>
                    <td><button class="btn btn-danger" ng-click="remove(product._id)">Remove</button></td>

                </tr>
            </tbody>

controller.js

$scope.remove = function(object) {
        $http({ 
                url: 'http://localhost:7200/api/manage-product', 
                method: 'DELETE', 
                data: {_id: object.id}, 
                headers: {"Content-Type": "application/json;charset=utf-8"}
        }).then(function(res) {
            console.log(res.data);
        }, function(error) {
            console.log(error);
        });
    };

node.js

router.delete('/manage-product/:_id', function(req,res){

    var db = req.db;

    var _id = req.params._id.toString();
    var collection = db.get('proInfo');

    collection.remove({"_id":_id}, function(err, result) { 
        res.send( (result === 1) ? { msg: 'Deleted' } : { msg: 'error: '+ err } );
    });

});

【问题讨论】:

  • 您的router 路径上没有/api/ 前缀?此外,请考虑使用$resource 而不是$http 与 RESTful 服务交互。

标签: angularjs node.js mongodb


【解决方案1】:

使用下面的代码,

  $http({ 
            url: 'http://localhost:7200/api/manage-product/'+object.id, 
            method: 'DELETE'  

    })

而不是

   $http({ 
            url: 'http://localhost:7200/api/manage-product', 
            method: 'DELETE', 
            data: {_id: object.id}, 
            headers: {"Content-Type": "application/json;charset=utf-8"}
    })

【讨论】:

    【解决方案2】:

    使用下面的代码我解决了这个问题。

    controller.js

         $scope.remove = function(_id,rindex) {     
            alert('Are you sure you want to delete?');
            $http({
                url: 'http://localhost:7200/api/manage-product',
                method: 'POST',
                data:{productId: _id}, 
            }).success(function(res) {
                console.log('another success');
            }, function(error) {
                console.log(error);
                alert('here');
            });
        }
    

    node.js

    router.post('/manage-product', function(req, res){
            console.log('I received another get request');
    
            var removeproducts = function(db, callback) {
               db.collection('proInfo').deleteOne({_id : ObjectId(req.body.productId)},
                  function(err, results) {
                     console.log('product deleted');
                     callback();
                  }
               );
            };
    
            MongoClient.connect(url, function(err, db) {
              assert.equal(null, err);
    
              removeproducts(db, function() {
                  db.close();
              });
            });         
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多