2017-04-06 160 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

相關問題