无法更新mongolab数据

时间:2013-10-28 18:39:46

标签: mongodb angularjs mlab

我正在使用angularjs,结合mongolab,来创建一个基本的应用程序。

Here is my code

问题是,我试图更新日期(当我点击“editer”时),但是当我提交表单时,得到了错误

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

更新方法是定义的,所以我不理解错误,任何帮助?

1 个答案:

答案 0 :(得分:2)

您可以稍微更改项目工厂:

评论后

更新

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;

    });

EditCtrl你做了一件奇怪的事情:

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

调用Project.instance()将返回没有任何update()方法的普通资源对象!查看Project factory中的代码。

同时更改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');
            });
        };
    }

最后你会让它发挥作用。

请参阅plunker