Question

I'm using angularjs, combined with mongolab, to create a basic application.

Here is my code

The problem is, i tried to update the date (when i click on "editer"),but when i submit the form, got this error :

TypeError: Object #<Resource> has no method 'update'

The update method is defined, so i don't understant the error, any help ?

Was it helpful?

Solution

You can change your Project factory a little bit:

updated after comment

angular.module('mongolab', ['ngResource']).
    factory('Project', function($resource) {
        var ProjectApi = $resource('https://api.mongolab.com/api/1/databases' +
                '/villes/collections/universities/:id',
                {apiKey: '7pcSaEmpVzZ90V6CVggeHa6Ys71uaZOA'}, {
            update: {method: 'PUT'}
        }
        );

        var Project = function(data) {
            angular.extend(this, data);
        };

        Project.query = ProjectApi.query;
        Project.get = ProjectApi.get;

        Project.prototype.update = function(cb) {
          return ProjectApi.update({id: this._id.$oid},
                angular.extend(this, {_id: undefined}), cb);
        };
        Project.prototype.destroy = function(cb) {
          return ProjectApi.remove({id: this._id.$oid}, cb);
        };
        return Project;

    });

In EditCtrl you make a strange thing:

function EditCtrl($scope, $location, $routeParams, Project) {
   var self = this;
   // Project is already defined in arguments and it is a service 
   var Project = Project.instance();
}

Calling Project.instance() will return you plain resource object that doesn't have any update() method! Look at your code in Project factory.

Also change code in EditCtrl:

function EditCtrl($scope, $location, $routeParams, Project) {
        var self = this;
        Project.get({id: $routeParams.projetId}, function(projet) {
            self.original = projet;
            $scope.projet = new Project(self.original);
        });

        $scope.save = function() {
            $scope.projet.update(function() {
                $location.path('/list');
            });
        };
    }

And finally you will have it working.

See plunker

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top