从两个http req获取数据并合并它们

我有两个模式在mongoose一个医生信息和两个排名。 我的医生架构:

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"); } }]); 

然后我把所有的服务器端都发给服务器端,我有服务器端计算排名的function(node.js)根据每个医生得到的calSchema函数计算出它们的排名并发回给客户端(controller)排行。 我得到的排名后,我想显示我的所有数据屏幕,但我有angular度同步的问题,打印我所有的医生和打印后,我的排名,我想一起打印,我需要做些什么来修复它?

谢谢,

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

 $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. 将医生推入可视arrays

我会build议使用$ q ,可能需要一点调整。 希望能帮助到你。

可以帮助你asynchronous运行函数

 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 }) });