I have a user list that I need to display. Each user has unread messages and has not created a meal list yet. I want to make two http.get requests within the main http.get request to retrieve the necessary information, but I am facing an issue with asynchronous execution, causing the values for unread messages and weeks to not update correctly. How can I resolve this? Thank you in advance.
$http.get('http://example.com/users').success(function(response){
var data=response;
if(data==null || data.length<=0){
return;
}
var date2 = new Date();
var users=[];
for(var i=0;i<data.length;i++){
var diffDays=0;
if(data[i].uyelikBaslangicTarihi=="0"){
continue;
}
var from = data[i].uyelikBaslangicTarihi.substring(0,10).split("/");
var date1 = new Date(from[2], (from[1] - 1), from[0]);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
if(diffDays>28 || diffDays<0){
continue;
}
var week=diffDays/7;
week=parseInt(week);
if(diffDays/7>0)
week++;
if(week!=0)
week--;
var unloadweek=week+1;
var unreadmessage=0;
$http.get('http://example.com/menus?userId=' + data[i]._id + '&haftaSayisi=' + week).success(function(response){
if(response==null || response.length==0){unloadweek= "";}
else{unloadweek= week;}
});
$http.get('http://example.com/sorus?userId=' + data[i]._id + '&okundu=0').success(function(response){
console.log(response);
if(response==null){ unreadmessage= 0;}
else{unreadmessage = response.length;}
});
var color="white"
var gender='mars'
if(data[i].cinsiyet=="Kadın")
gender='venus';
var user={
id:data[i]._id,
name:data[i].ad,
gender:gender,
weight:data[i].kilo,
height:data[i].boy,
birthday:data[i].dogum_tarihi,
email:data[i].email,
src:"http://example.com/images/"+data[i]._id+".jpeg",
color:color,
week:unloadweek,
unreadmessage:unreadmessage
}
if(data[i].uyelikBaslangicTarihi!="" && data[i].uyelikBaslangicTarihi!="0" && data[i].rol!="admin")
users.push(user);
}
$scope.customers=users;
})
.error(function(err){
alert("hata");
}
)