I used nodejs, mongoose and angular to implement RestAPI with mean stack, I tried to update provider record in my database. Here you can find what i have in put method: I have my Schema description here This is what i am getting in console.
Object {_id: "553d27a24c47696420c6ee39", name: "nn", url: undefined, __v: 0, posts: Array[0]…}
This API working if u enter data through postman.
/*update the provider by id in REST API*/
router.put('/providers/:provider/edit', function (req, res) {
var id = req.params.provider;
var query = {"_id": id};
var update = {
name: req.body.name,
url: req.body.url
};
var options = {new: true};
Provider.findOneAndUpdate(query, update, options, function (err, provider) {
if (err) { return next(err); }
res.json(provider);
});
})
Here, find correlated factory:
ob.updateProvider = function (provider) {
return $http.put(urlBase +'/providers/' + provider._id + '/edit', provider)
.success(function(provider){
ob.providers.push(provider);
// angular.copy(provider, ob.providers);
})
};
This part is inside Modal controllers :
$scope.updateProvider = function (updatedProvider) {
var provider = updatedProvider;
providers.updateProvider(provider)
.success(function () {
$scope.status = 'Updated provider.';
}).
error(function(error) {
alert('Unable to update provider: ' + error);
});
};
Also i used Modal from ui-bootstarpt which also inside BlogCtrl
var modalInstance = $modal.open({
templateUrl: 'update_provider_model.html',
controller: function ($scope, $modalInstance, provider){
$scope.provider = provider;
$scope.ok = function () {
$modalInstance.close($scope.provider);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
},
size: size,
resolve: {
provider: function () {
return provider;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
Finally, inside template update_provider_model.html ,in the rest part of this template i have ng-model="provider.name" , ng-model="provider.url"
<div class="modal-footer">
<button ng-click="updateProvider(provider);ok()" class="btn btn-success"><i class="icon-white icon-plus"></i> Update & Close</button>
<button ng-click="cancel()" class="btn btn-warning">Cancel</button>
</div>
In any condition, i am not able to call my api put method and it never execute. I don't get any error. I can not find what is wrong and which part have a problem. Therefore i can not save update record into database. Only i can see my change in the view and in my Modal.If somebody can suggest me correct solution i will be thank you.