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