mercredi 1 avril 2015

Node Server - res.redirect is using 'PUT' method. Need 'GET'

Using MEAN stack.


I created a mongoose call to update a document in my mongodb collection. That is working, no issue. However when the update is complete I want to "simply" redirect to my '/search' page via server controller.


The redirect works on my other mongoose save call, which is a simple 'POST'.


However when I call redirect after my 'PUT' the redirect is using PUT and not GET, resulting in a 404, that the method PUT /search can't be found. That would be correct!, however I'm not sure why its choosing 'PUT' method. Can I force a 'GET'?


My express routes are using passport, so all pages that require authentication go through my index.js. (see gist). My mongoose calls calls all go directly from my view or controller to the server via /api/... route.


Hoping this is a quick fix.


My code in a gist. http://ift.tt/1CaEJ5U


Client Controller - call to server to perform update (works)



$scope.updateProduct = function() {
//$scope.edited.inv = $scope.edited.searchSKU;
$scope.edited.inv.SKU = $scope.edited.SKU;
$scope.edited.inv.Product_Name = $scope.edited.Name;
$scope.edited.inv.Product_Description = $scope.edited.Desc;
$scope.edited.inv.Quantity = $scope.edited.quantity;
$scope.edited.inv.Product_Location = $scope.edited.locations;
//console.log('Edited: ' + JSON.stringify($scope.edited.inv));
//console.log('Not Edited: ' + JSON.stringify($scope.prod.currentProd));
var inventory = new Inventory($scope.edited.inv);
//console.log('edited.inv._id: ' + $scope.edited.inv._id);
inventory.$update({id : $scope.edited.inv._id}, inventory, function (err, results){
if(err){};
console.log(results);
});
}


Server Controller - Failing to redirect:



module.exports.editInventory = function(req, res){
Product.update({_id : req.body._id}, req.body, function (err, results){
if(err){
req.flash('message', 'Error Updating Product');
console.log('Error Updating Product: ' + err);
throw err;
}
req.flash('message','Product Updated Successfully!');
res.redirect('/search');
});


};


Let me know if you need additional info.


Aucun commentaire:

Enregistrer un commentaire