2017-04-06 157 views
1

我有两个模式,一个用于医生信息,另一个用于排名。 我的医生架构:从两个http req获取数据并将它们合并

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并计算他们的排名并发回客户端(控制器)的排名。 我得到的排名后,我想显示我所有的数据到屏幕上, 但我有角度同步问题,它打印我所有的医生,并在打印排名后,我想一起打印它们, 我需要什么要解决它吗?

感谢,

+0

它看起来像医生的整个阵列上运行的功能被发送到 “calculateRanking” API。这发生在每个医生身上。你的意思是一次只派一名医生到医院吗? –

+0

是的,这正是我的意思 – adi

回答

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. 对于每位医生,请拨打电话以获得排名
  4. 将医生的排名
  5. 推到医生可视阵列
+0

谢谢你,它的工作很好!感谢 – adi

0

我会建议使用$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 
    }) 
}); 
+0

,但给我工作之前的建议 – adi

相关问题