【问题标题】:Query MongoDB with Mongoose within Angular App在 Angular 应用程序中使用 Mongoose 查询 MongoDB
【发布时间】: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


    【解决方案1】:

    我花了一些时间告诉你你的结构应该是什么样子。 服务器上的 API 处理程序应如下所示:

    app.get('api/businesses', function(req, res) {
      Businesses.find({specialty: req.query.type})
      .then(function(businesses){
        res.json(businesses);
      })
      .catch(function(error){
        console.log("There was error retrieving businesses" + error);
      });
    });
    

    在前端,进行 http 调用的工厂应如下所示:

    angular.module('queryService', [])
    .factory('queryFactory', function($http){
      var getBusinesses = function(type) {
        return $http({
          method: 'GET',
          url: 'api/businesses?type=' + type
        })
      };
    
      return {
        getBusinesses: getBusinesses
      }
    });
    

    并且控制器必须在响应返回后对数据做一些事情:

    angular.module('UserCtrl', [])
    
    .controller('UserSelectController', function($scope, queryFactory) {
      $scope.listOfTypes = ['Type 1', 'Type 2', 'Type 3', 'Type 4', 'Type 5', 'Type 6', 'Type 7', 'Type 8'];
    
      $scope.getBusiness = function(){
      queryFactory.getBusinesses($scope.selectedType)
        .then(function(response){
        // do something with response.data
        // put it on $scope.businesses
        });
      };
    });
    

    【讨论】:

    • 我的 API 处理程序应该有“getCompanies”吗?我问只是因为通常我在那里只看到一个匿名函数。
    • 当我阅读问题时,我看不到companiesgetCompanies等的相关性,并且端点api/companies?type='完全错误!
    • DLF85,你说得对,那里应该是匿名函数,后来我加了路由,忘记改了。我还将“公司”重命名为“企业”以匹配您的代码。
    • 谢谢!现在它更有意义了
    • @davidkonrad 进行更改后仍然错误吗?如果不能,您能否进一步说明您的立场?
    【解决方案2】:

    'businesses' 未定义,因为它尚未分配。您的代码缺少任何用于检索数据的服务器调用。你需要:

    1. 获取数据的 REST 调用
    2. 建议在服务器调用中传递“类型”,以便端点仅返回所需的数据。
    3. queryType 应返回一个承诺 ($q),该承诺在返回数据时解决。

    【讨论】:

    • 我想我的误解来自认为我可以直接访问我的数据库。我现在刚刚创建了一个对服务器的 GET 请求并设置了一个条件,以便返回的唯一内容是与类型匹配的内容。不知何故,我觉得我做了这个“漫长”的方式,我没有正确使用猫鼬。我仍然在渲染从我的 GET 请求发回的数据时遇到问题。
    • 听起来你正确使用了Mongoose。它简化了与 MongoDB 的服务器端交互。因此,为了帮助我们帮助您解决问题 - 获取从示例返回的 json,并在 plunker 或 codepen 中创建您的渲染问题示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多