1

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.

Community
  • 1
  • 1
pm1359
  • 622
  • 1
  • 10
  • 31
  • Have you confirmed in browser dev tools network tab that request is not being sent? If it is check status and what is sent is what you expect to be sent and check response body – charlietfl Apr 26 '15 at 17:20
  • Yes,Of course. It is not executing at all. Only execute my modal.html and then stop. I am not sure if my code is correct or not for this purpose. @charlietfl – pm1359 Apr 26 '15 at 17:28
  • Doesn't look like you used `controllerAs` for modal controller. So does `cancel()` work? If so note you have `BlogCtrl.updateProvider` in markup. but not `BlogCtrl.cancel()` I think you are getting scopes mixed up. You don't have a way in modal controller to make the request, so could make it in result callback – charlietfl Apr 26 '15 at 17:35
  • They are all located inside one controller called BlogCtrl. I updated my question@ charlietfl – pm1359 Apr 26 '15 at 17:40
  • but your modal is a different controller defined in `modalinstance` – charlietfl Apr 26 '15 at 17:43
  • Yes, cancel button is working and ok as well. The problem with calling put from API which never called. Then what is your suggestion? – pm1359 Apr 26 '15 at 17:45
  • That updateProvider method doesn't exist in modal controller – charlietfl Apr 26 '15 at 17:46
  • can call it in the modal result callback. Only at that point the modal is already closed by `ok()` so might want to close modal on success of the update and get rid of `ok()` – charlietfl Apr 26 '15 at 17:54
  • That was the problem.(y) I have moved it into this scope and now is working. But what about if i want to keep it outside Modal controller. Also problem is still remaining with update function where i can update only name property and link function is undefined remains. – pm1359 Apr 26 '15 at 17:56
  • back to console in browser to see what gets sent, returned etc. Then do some node console logging – charlietfl Apr 26 '15 at 17:57

1 Answers1

0

I keep this question. It might be useful for some of you. I would like to add what was the problem with url property. I have defined Url property type="url". However when i tried it with some random text i put value equal text not url so it returns me undefined and never stored that value in the variable on database. Thank you of @charlietfl for your help.

pm1359
  • 622
  • 1
  • 10
  • 31