【发布时间】:2016-08-25 07:44:56
【问题描述】:
我有一个应用程序需要查询我的 Mongo 数据库并返回所有符合指定条件的项目,以便将它们呈现到 DOM。我已经通过我的应用程序使用邮递员和本地测试了数据库,因此我确定所有信息都被正确存储。我的问题是我不完全知道查询应该在我的应用程序中发生的位置。
用户将使用下拉菜单来指定业务类型,一旦完成,所有匹配此类型的业务都应填充 DOM。以下是我到目前为止的代码:
这是用户控制器:
angular.module('UserCtrl', [])
.controller('UserSelectController', function($scope, queryFactory) {
//list out all of our specialty types of food in the below array -- this will populate in our dropdown selection for our user_BusDirectory.html view
$scope.listOfTypes = ['Type 1', 'Type 2', 'Type 3', 'Type 4', 'Type 5', 'Type 6', 'Type 7', 'Type 8'];
//invoke function to call a GET request to get business with that type of specialty
$scope.getBusiness = function(){
console.log('You selected: ', $scope.selectedType);
queryFactory.queryType($scope.selectedType);
};
});
以下驻留在我的工厂:
angular.module('queryService', [])
.factory('queryFactory', function($http){
var queryType = function(type){
console.log('What is the type that has been passed in: ',type)
var query = businesses.find({specialty: type}).exec(function(err, businessMatches){
if(err){
console.log(err);
return res.send({ errorMessage : err})
}else{
res.json(businessMatches);
}
});
console.log("Did query recieve all the types? :", query);
}
return {
queryType: queryType
}
});
在我的 Mongo 数据库中 businesses 是我要查询的集合的名称。当我尝试测试该功能时,我不断收到ReferenceError: businesses is not defined,这让我相信我的方法被误导了。
【问题讨论】:
标签: angularjs mongodb mongoose mean-stack mongoose-populate