Node.js 我将包注入angular模块,为什么我的包为空?
我正在使用mongoose将对象存储到我的数据库中。我已经成功地将一个对象保存到数据库中,现在每当我尝试搜索它时,我都会得到一个未定义的组合。queryfunction…我有一个称为组合的模块,这是它的控制器:Node.js 我将包注入angular模块,为什么我的包为空?,node.js,angularjs,mongoose,mean-stack,Node.js,Angularjs,Mongoose,Mean Stack,我正在使用mongoose将对象存储到我的数据库中。我已经成功地将一个对象保存到数据库中,现在每当我尝试搜索它时,我都会得到一个未定义的组合。queryfunction…我有一个称为组合的模块,这是它的控制器: angular.module('mean.compositions').controller('CompositionsController', ['$scope','fileUpload' ,'$stateParams', '$location', 'Global', 'Composi
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');
};
}
现在,每当我尝试查询组合时,都会出现以下错误:
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">
感谢您提供我为什么会遇到这个奇怪的错误的原因。Composition是由您使用以下代码创建的服务:
"严格使用",;
角度。模块“平均成分”。工厂“成分”[
作用{
返回{
姓名:“作文”
};
}
];
它只有一个名为name的属性,它的值为。您试图做的是将CompositionService注入CompositionController,并在其上调用查询方法。记住这一点,这并不是一个谜,为什么整个事情在未定义的情况下失败不是一个函数错误
您可能需要做的是向Compositions factory函数返回的对象添加一个查询方法,并从中返回一些有意义的内容
返回{
名称:'组合',
查询:函数{
console.log这是我必须访问DB'的地方;
返回['object1','object2','object3']}
};
你能发布合成服务吗?@AnthonyChu我已经用服务代码更新了我的帖子。对不起,我指的是注入控制器的合成服务。这是什么?它是如何定义的?@AnthonyChu我对angular很陌生,但我认为新的更新应该是你所要求的?是的,就是这个。组合只是一个具有name属性的对象。这就解释了为什么当您试图调用它不存在的查询方法时会出现错误。您的意思是从工厂返回资源吗?这很有效,但我使用了我定义的$resource依赖项。谢谢
'use strict';
angular.module('mean.compositions').factory('Compositions', [
function() {
return {
name: 'compositions'
};
}
]);