【问题标题】:Get data from two http req and combine them从两个 http req 中获取数据并将它们组合起来
【发布时间】:2017-09-01 18:55:16
【问题描述】:

我在猫鼬中有两个架构,一个用于医生信息,两个用于排名。 我的医生架构:

var doctorsSchema = new schema({
  Entity: {type:String},
  name: {type:String},
  Expertise : {type:String},
  HMO: {type:String},
  Address: {type:String},
  reception_hours: [],
  lat: {type:Number},
  lng: {type:Number},
  Ranking : {type:Number}
},{collection: 'doctorsLocation'});

我的排名架构是:

var calSchema = new schema({
  Entity: {type:String},
  name: {type:String},
  Expertise : {type:String},
  Address: {type:String},
  Attention : {type:Number},
  Professional: {type:Number},
  Availability: {type:Number},
  Atmosphere: {type:Number},
  Recommendation: {type:Number}
},{collection: 'cal'});

我想计算每个医生的排名,然后将每个医生的所有详细信息打印到屏幕上。

这是我的控制器(在 angularjs 中):

mymedical.controller('calCtrl',['$scope','$http','$cookies', function($scope,$http,$cookies){
var generalInfo = Array();

  $http.get('http://localhost:3000/doctors').then(function(response){
      //console.log(response);
      var generalData = response.data;

    for(var i=0; i<generalData.length; i++){
    $http.post('http://localhost:3000/calculateRanking',generalData).then(function(res){
            //console.log(res.data);
            var info ={};
            info.Ranking = res.data;
            //console.log(res.data);
            console.log("1");
            info.Entity = generalData[i].Entity;
            info.name = generalData[i].name;
            info.Expertise = generalData[i].Expertise;
            info.Address = generalData[i].Address;

            info.reception_hours=generalData[i].reception_hours;
            info.HMO=generalData[i].HMO;
            generalInfo.push(info);
        }).then(last(generalInfo));
    }

                  //info.Ranking = 0;
          //console.log(rank);


  });

function last(val)
{
  $scope.general = val;
    //console.log(generalInfo);
    console.log("last");
}


}]);

我将所有医生都发送到服务器端,然后, 我有计算服务器端排名的功能(node.js) 函数获取 calSchema 并根据每个医生计算他们的排名并将排名发送回客户端(控制器)。 获得排名后,我想将所有数据显示到屏幕上, 但是我有角度同步的问题,它打印我所有的医生,然后打印我的排名,我想把它们打印在一起, 我需要做什么来修复它?

谢谢,

【问题讨论】:

  • 看起来整个医生数组都被发送到“calculateRanking”api。每个医生都在发生这种情况。你的意思是一次只派一名医生去吗?
  • 是的,正是我的意思

标签: javascript angularjs node.js mongoose schema


【解决方案1】:

如果我理解正确,我想这可能就是你想要的

$http.get('http://localhost:3000/doctors').then(function(doctors) {
        angular.forEach(doctors.data, function(doctor) {
            $http.post('http://localhost:3000/calculateRanking', doctor)
                .then(function(res) {
                    doctor.Ranking = res.data;
                    $scope.general.push(doctor);
                });
        });
    });

这样会

  1. 获取所有医生
  2. 遍历医生
  3. 为每位医生调用 API 以获取他们的排名
  4. 设置医生排名
  5. 将医生推入可视阵列

【讨论】:

    【解决方案2】:

    我建议使用$q,可能需要稍作调整。希望对您有所帮助。

    帮助您异步运行函数

    function postDocData(doc) {
      return $http
        .post('http://localhost:3000/calculateRanking', doc)
        .then(function(res) {
          doctor.Ranking = res.data;
          $scope.general.push(doctor);
        });
    }
    
    $http.get('http://localhost:3000/doctors').then(function(doctors) {
      var docPromiseArray = [];
    
      angular.forEach(doctors.data, function(doctor) {
        docPromiseArray.push(postDocData(doctor));
      });
    
      $q.all[docPromiseArray]
        .catch(function(err) {
          //error handling
          console.log(err);
        })
        .finally(function() {
          //more processing
        })
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2019-10-04
      • 1970-01-01
      相关资源
      最近更新 更多