0

This may not be a perfect solution, but I have a ProductModel where some products have a path to a locally stored image, image01Path. When I want to delete a product I am trying to do firstly delete the image from local storage, then trying to delete the product document.

My 'delete' service initially looks like -

app.delete('/api/product/:id', function(req, res){
    ProductModel.findOne({_id:req.params.id}).exec(function(err, product){
        if (!err){ 
            if(product.image01Path){
                fs.unlink('public/' + product.image01Path);
            }
            ProductModel.findOne({_id:req.params.id}).remove().exec().then(function successCallback(data){
                res.send(200);
            }, function errorCallback(){

            }); 
        } else {throw err;}
    });

})

Which is being called by -

$scope.delete = function(product){
       $http.delete('/api/product/' + $routeParams.id).then(function successCallback(){
           $location.url('/home');
       }, function errorCallback(){

       })
   }  

My issue is, even though I believe I have the redirect to home only happening on success of the deletion of the document, when my Home controller makes another rest call to get the updated list of products I am still seeing the deleted product. When I then refresh the page I am finally seeing the correct list of products. Where am I going wrong?

My Home controller -

app.controller('HomeCtrl', function($rootScope, $scope, $http, $location){   
    $http.get('/api/products').then(function successCallback(products){
        $scope.products = products.data;       
    }, function errorCallback(){

    });
})
andy mccullough
  • 9,070
  • 6
  • 32
  • 55

3 Answers3

1

Found the issue - I had both href="#" and ng-click="delete(product)" in the view. It looks like the href was redirecting me to home which was making the rest call for a list of products, as per my Home controller above, before the product had been deleted in the back end.

Looking at this question - href overrides ng-click in Angular.js I see what I should have done.

Community
  • 1
  • 1
andy mccullough
  • 9,070
  • 6
  • 32
  • 55
0

It may be because fs is async as well. So you'll need to pass in a callback into the fs.unlink (rough coding):

app.delete('/api/product/:id', function (req, res) {
ProductModel.findOne({ _id: req.params.id }).exec(function (err, product) {
    if (!err) {
        if (product.image01Path) {
            fs.unlink('public/' + product.image01Path, function (err) {

                if (!err) {
                    ProductModel.findOne({ _id: req.params.id }).remove().exec().then(function successCallback(data) {
                        res.send(200);
                    }, function errorCallback() {

                    });
                } else {
                    //fs.unlink failed
                }
            });
        } else {
            //Product.image not found
        }
    } else {
        //Error in finding product
        throw err;
    }
});
});
Peheje
  • 12,542
  • 1
  • 21
  • 30
  • Thanks for the input - I had tried this out, but doesnt seem to solve the issue. Note the file is pretty standalone and should be able to be remove async, the product only has a String path to where the file should be located – andy mccullough Aug 09 '16 at 19:25
0

unlink method from fs is Asynchronous as mentioned here, so you have you options.

Use the Synchronous unlink:

fs.unlinkSync(path)

Or use the callback param in Asynchronous unlink, so it will be:

if(product.image01Path){
    fs.unlink('public/' + product.image01Path, function(){
        ProductModel.findOne({_id:req.params.id}).remove().exec().then(function successCallback(data){
            res.send(200);
        }, function errorCallback(){
        }); 
    });
}
Fernando Del Olmo
  • 1,440
  • 17
  • 32
  • Thanks for the input - I had tried this out, but doesnt seem to solve the issue. Note the file is pretty standalone and should be able to be remove async, the product only has a String path to where the file should be located – andy mccullough Aug 09 '16 at 19:25