我将包注入angular模块,为什么我的包为null?

时间:2014-07-13 06:00:57

标签: node.js angularjs mongoose mean-stack

我正在使用mongoose将对象存储到我的数据库中。我已经成功地将一个对象保存到数据库中,现在每当我尝试搜索它时,我在“Compositions.query(function ...”)上得到一个未定义的。我有这个模块,称为组合,这是它的控制器:

angular.module('mean.compositions').controller('CompositionsController', ['$scope','fileUpload' ,'$stateParams', '$location', 'Global', 'Compositions',
  function($scope, fileUpload, $stateParams, $location, Global, Compositions) {
    $scope.global = Global;
    $scope.package = {
      name: 'compositions'
    };
$scope.find = function() {
      console.log('get here');
      Compositions.query(function(composition) {
        console.log(composition);
        $scope.composition = composition;
      });
      console.log('pass');
    };
}

现在,每当我尝试查询Compositions时,都会收到此错误:

undefined is not a function
    at Scope.$scope.find (http://localhost:3000/modules/aggregated.js:6:644)
    at http://localhost:3000/bower_components/angular/angular.js:10735:21
    at Scope.$eval (http://localhost:3000/bower_components/angular/angular.js:12595:28)
    at pre (http://localhost:3000/bower_components/angular/angular.js:19781:15)
    at nodeLinkFn (http://localhost:3000/bower_components/angular/angular.js:6628:13)
    at compositeLinkFn (http://localhost:3000/bower_components/angular/angular.js:6039:13)
    at publicLinkFn (http://localhost:3000/bower_components/angular/angular.js:5934:30)
    at http://localhost:3000/bower_components/angular-ui-router/release/angular-ui-router.js:2805:9
    at nodeLinkFn (http://localhost:3000/bower_components/angular/angular.js:6648:13)
    at compositeLinkFn (http://localhost:3000/bower_components/angular/angular.js:6039:13) <section data-ng-controller="CompositionsController" data-ng-init="find()" class="ng-scope">

我已经包含了服务代码。

更新:

 'use strict';

angular.module('mean.compositions').factory('Compositions', [
    function() {
        return {
            name: 'compositions'
        };
    }
]);

感谢您提出任何意见,为什么我会收到这个奇怪的错误。

1 个答案:

答案 0 :(得分:0)

Composition是您使用以下代码创建的服务:

'use strict';

angular.module('mean.compositions').factory('Compositions', [
    function() {
        return {
            name: 'compositions'
        };
    }
]);

它唯一拥有的是名为name的属性,其值为compositions。您要做的是将Composition服务注入CompositionsController并在其上调用query方法。牢记这一点,为什么整个事情因undefined is not a function错误而失败,这不是一个让人感到困惑的事。

可能需要做的是向query工厂函数返回的对象添加Compositions方法,并从那里返回有意义的东西。

return {
    name: 'compositions',
    query: function() { 
        console.log("This is where I have to access my DB'); 
        return ['object1', 'object2', 'object3'] }
    };