1

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 ?

Amith
  • 6,818
  • 6
  • 34
  • 45
user
  • 539
  • 1
  • 11
  • 32

1 Answers1

2

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

Peter Gerasimenko
  • 1,906
  • 15
  • 13
  • This is how i builded my code at first before changing it. The problem with this code is, when i try to display the data of each record with `
    {{key}}:{{val}}
    ` i have 2 extra lines : update and destroy (see in the "editer" view), but i just want to display the mongolab fields (id, name, rang)
    – user Oct 29 '13 at 12:45
  • 1
    I've updated `Project` factory in my answer. Of course, it's only a way to do so. I think this nice thread about prototype in javascript will be helpful too http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work . – Peter Gerasimenko Oct 30 '13 at 05:33