Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to delete data in mongodb using angularjs and node.js but Cannot DELETE /api/manage-product error is coming in console.

.html file

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

});

console error image

share|improve this question
    
Your router doesn't have the /api/ prefix on the path? Also, consider using $resource instead of $http for interfacing with RESTful services. – Alnitak Feb 16 '16 at 10:30

Use below code,

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

    })

instead of

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

Using the following code i fixed the problem.

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();
          });
        });         
    });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.