javascript - Get data from two http req and combine them -
i have 2 schema in mongoose 1 doctors info , 2 ranking. doctors schema :
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'}); my ranking schema is:
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'}); i want calculate ranking of each doctor , after print details of each doctor screen.
this controller (in 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"); } }]); i send server side doctors , then, have function calculate ranking in server side(node.js) function calschema , on according each doctor calculate them ranking , send client side (controller) ranking. after ranking want display data screen , have problem sync of angular print me doctors , after print me ranking, , want print them together, need fix it?
thanks,
if understand correctly, think may want
$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); }); }); }); so will
- get doctors
- loop through doctors
- for each doctor, make api call ranking
- set doctor's ranking
- push doctor viewable array
Comments
Post a Comment