How to do a function after http.get method finished!

I have 2 function:
loadDanhSachHoSoChuaGiaiQuyetThangHienTai(){
return this.http.get(this.danhSachHoSoChuaGiaiQuyet_Url+“thangBaoCao=”+this.month+"&namBaoCao="+ this.year +"&nguoidungID="+this.GlobalProvider.MaNhanVien+"&sobannganhID="+this.soBanNganhID)
.subscribe(
Data =>{
this.danhSachHoSoChuaGiaiQuyetThangHienTai = Data.json();
console.log(this.danhSachHoSoChuaGiaiQuyetThangHienTai);
},err => {
console.log(‘Lỗi = >’+ err);
});
}
loadDanhSachHoSoChuaGiaiQuyetThangTruoc(){
return this.http.get(this.danhSachHoSoChuaGiaiQuyet_Url+“thangBaoCao=”+this.month +"&namBaoCao="+this.year+"&nguoidungID="+this.GlobalProvider.MaNhanVien+"&sobannganhID="+this.soBanNganhID)
.subscribe(
Data =>{
this.danhSachHoSoChuaGiaiQuyetThangTruoc = Data.json();
console.log(this.danhSachHoSoChuaGiaiQuyetThangTruoc);
},err => {
console.log(‘Lỗi = >’+ err);
});
}
AND I DO THIS IN THE
CONSTRUCTOR(){
try {
this.loadDanhSachHoSoChuaGiaiQuyetThangHienTai();
this.loadDanhSachHoSoChuaGiaiQuyetThangTruoc();
} catch (e){
console.log((e).message);//conversion to Error type
}
}

AND IN THE
ionViewDidEnter(){
if(this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_1 != 0 || this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_2 !=0 ){
// do something
forEach the array load form http.get will be work here !

}
}

but when i do the “if” inside ionViewDidEnter() function not work,
the result Cannot read property ‘forEach’ of undefined

i found a lot of topic and find the reason that it : this array “danhSachHoSoChuaGiaiQuyetThangTruoc” is undefined because the “if” run before http.get finish

Please help me how to take the “if” run after http.get finish
thanks in advanced!

Always declare the type of all parameters and of the return value of all methods.

When a method returns something, ask yourself “what do I want to do with this?”. If the initial answer is “idk, nothing i guess”, then say to yourself “so I’m treating this method call like throwing a message in a bottle. i don’t have any idea when it will do what it does, and that’s OK”.

When you carry out this exercise with the code you’re describing here, I have a feeling you will not be OK with ignoring the return values of some of these functions, and that should lead you to how you can restructure this code to work as you desire.

1 Like

Hi sir ! I want to load a array from http.get and then using that array. But the array is null or undefine because the http.get not finish before i using the “if” function.

What does the code look like now, after you’ve completely gone through the process described in my earlier post?

1 Like

I want the http.get finish before i run this function:
testHam() {
this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_1 = 0;
this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_2 = 0;
// duyệt hết danh sách hồ sơ của nhân viên đó trong tháng hiện tại
this.danhSachHoSoChuaGiaiQuyetThangHienTai.forEach(element => {
var NgayHenTraHs = new Date(element.NgayHenTra);
console.log(this.tinhSoNgay(NgayHenTraHs,this.NgayHienTai));
if(this.NgayHienTai < NgayHenTraHs ){// kiểm tra xem cái ngày trả có vượt qua ngày hiện tại chưa
if(this.tinhSoNgay(NgayHenTraHs,this.NgayHienTai)<=5){ // kiểm tra điều kiện ngày trả từ thời gian hiện tại đến ngày trả là 5 ngày
this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_1=+1;
}
}
});
// duyệt hết danh sách hồ sơ của nhân viên đó trong tháng trước
this.danhSachHoSoChuaGiaiQuyetThangTruoc.forEach( data => {
var NgayHenTraHs = new Date(data.NgayHenTra);
console.log(this.tinhSoNgay(NgayHenTraHs,this.NgayHienTai));
if(this.NgayHienTai < NgayHenTraHs ){// kiểm tra xem cái ngày trả có vượt qua ngày hiện tại chưa
if(this.tinhSoNgay(NgayHenTraHs,this.NgayHienTai)<=5){// kiểm tra điều kiện ngày trả từ thời gian hiện tại đến ngày trả là 5 ngày
this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_2=+1;
}
}
});
// thông báo số lượng hồ sơ đã trễ hạn
if(this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_1 != 0 || this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_2 !=0 ){
this.toast.create({
message: (this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_1 + this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_2) + Hồ sơ đến hạn cần xử lý,
duration: 5000,
position: ‘top’,
closeButtonText: “OK”,
showCloseButton: true
}).present();
}else {
this.toast.create({
message: ‘Không có hồ sơ nào đến hạn’,
duration: 5000,
position: ‘top’,
closeButtonText: “OK”,
showCloseButton: true
}).present();
}
}

“forEach” is not work because the http.get is not finish so “this.danhSachHoSoChuaGiaiQuyetThangTruoc” is undefined.

all the code of this *.ts file
import { Component } from ‘@angular/core’;
import { IonicPage, NavController, NavParams } from ‘ionic-angular/’;
import { ManHinhTheoDoiTinhHinhPage } from ‘…/man-hinh-theo-doi-tinh-hinh/man-hinh-theo-doi-tinh-hinh’;
import { HomePage, chiTietHoSo } from ‘…/home/home’;
import { ManHinhTraCuuPage } from ‘…/man-hinh-tra-cuu/man-hinh-tra-cuu’;
import { ThongKeNguoiDungPage } from ‘…/thong-ke-nguoi-dung/thong-ke-nguoi-dung’;
import {ThongKePhongBanPage} from ‘…/thong-ke-phong-ban/thong-ke-phong-ban’;
import {ThongKeLinhVucPage} from ‘…/thong-ke-linh-vuc/thong-ke-linh-vuc’;
import {BieuDoThongKePage} from ‘…/bieu-do-thong-ke/bieu-do-thong-ke’;
import { Http } from ‘@angular/http’;
import {ToastController } from ‘ionic-angular/’;
import { NativeStorage } from ‘@ionic-native/native-storage’;
import { GlobalProvider } from ‘…/…/providers/global/global’;
import { DanhSachDenHanCanGiaiQuyetCuaNhanVienPage } from ‘…/danh-sach-den-han-can-giai-quyet-cua-nhan-vien/danh-sach-den-han-can-giai-quyet-cua-nhan-vien’;

@IonicPage()
@Component({
selector: ‘page-man-hinh-chinh’,
templateUrl: ‘man-hinh-chinh.html’,
})
export class ManHinhChinhPage {
private NgayHienTai = new Date();
private month = this.NgayHienTai.getMonth() + 1;
private year = this.NgayHienTai.getFullYear();
private day = this.NgayHienTai.getDate();
public danhSachHoSoChuaGiaiQuyet_Url = this.GlobalProvider.HOST_API_URL +"/api/HoSoChuaGiaiQuyetNguoiDungThangNamBatKy?";
public danhSachHoSoChuaGiaiQuyetThangHienTai:chiTietHoSo[ ] ;
public danhSachHoSoChuaGiaiQuyetThangTruoc:chiTietHoSo[ ] ;
public Email:string;
private soBanNganhID:string = ‘16’;
constructor(private GlobalProvider:GlobalProvider,private NativeStorage:NativeStorage,private toast:ToastController,private http:Http,public navCtrl: NavController, public navParams: NavParams) {
this.loadDanhSachHoSoChuaGiaiQuyetThangHienTai();
this.loadDanhSachHoSoChuaGiaiQuyetThangTruoc();
// this.danhSachHoSoChuaGiaiQuyetThangHienTai = this.navParams.get(‘danhSachHoSoChuaGiaiQuyetThangHienTai_Send’);
// this.danhSachHoSoChuaGiaiQuyetThangTruoc = this.navParams.get(‘danhSachHoSoChuaGiaiQuyetThangTruoc_Send’);
}
ionViewDidEnter(){
if(this.GlobalProvider.KIEM_TRA_THONG_BAO_DAY){
this.testHam();
this.GlobalProvider.KIEM_TRA_THONG_BAO_DAY = false;
}

}
ionViewDidLoad() {
console.log(‘ionViewDidLoad ManHinhChinhPage’);
console.log(this.GlobalProvider.MaNhanVien);
// this.Email = this.navParams.get(‘EmailSend’);
}
// hàm này dùng để cho nút test trên giao diện xóa ngay sau khi sử dụng xong
testHam() {
this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_1 = 0;
this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_2 = 0;
// duyệt hết danh sách hồ sơ của nhân viên đó trong tháng hiện tại
this.danhSachHoSoChuaGiaiQuyetThangHienTai.forEach(element => {
var NgayHenTraHs = new Date(element.NgayHenTra);
console.log(this.tinhSoNgay(NgayHenTraHs,this.NgayHienTai));
if(this.NgayHienTai < NgayHenTraHs ){// kiểm tra xem cái ngày trả có vượt qua ngày hiện tại chưa
if(this.tinhSoNgay(NgayHenTraHs,this.NgayHienTai)<=5){ // kiểm tra điều kiện ngày trả từ thời gian hiện tại đến ngày trả là 5 ngày
this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_1=+1;
}
}
});
// duyệt hết danh sách hồ sơ của nhân viên đó trong tháng trước
this.danhSachHoSoChuaGiaiQuyetThangTruoc.forEach( data => {
var NgayHenTraHs = new Date(data.NgayHenTra);
console.log(this.tinhSoNgay(NgayHenTraHs,this.NgayHienTai));
if(this.NgayHienTai < NgayHenTraHs ){// kiểm tra xem cái ngày trả có vượt qua ngày hiện tại chưa
if(this.tinhSoNgay(NgayHenTraHs,this.NgayHienTai)<=5){// kiểm tra điều kiện ngày trả từ thời gian hiện tại đến ngày trả là 5 ngày
this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_2=+1;
}
}
});
// thông báo số lượng hồ sơ đã trễ hạn
if(this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_1 != 0 || this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_2 !=0 ){
this.toast.create({
message: (this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_1 + this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_2) + Hồ sơ đến hạn cần xử lý,
duration: 5000,
position: ‘top’,
closeButtonText: “OK”,
showCloseButton: true
}).present();
}else {
this.toast.create({
message: ‘Không có hồ sơ nào đến hạn’,
duration: 5000,
position: ‘top’,
closeButtonText: “OK”,
showCloseButton: true
}).present();
}
}

truyenDuLieu(){
if(this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_1 != 0 || this.GlobalProvider.demSoLuongHosoTreCuaNhanVien_2 !=0 ){
// sau khi thông báo thì truyền danh sách qua màn hình hiển thị ngay tại đây
this.navCtrl.push(DanhSachDenHanCanGiaiQuyetCuaNhanVienPage,{
danhSachHoSoChuaGiaiQuyetThangHienTai_Send: this.danhSachHoSoChuaGiaiQuyetThangHienTai,
danhSachHoSoChuaGiaiQuyetThangTruoc_Send: this.danhSachHoSoChuaGiaiQuyetThangTruoc
});

}else{
this.toast.create({
message: ‘Không có hồ sơ nào đến hạn’,
duration: 3000,
position: ‘bottom’
}).present();
}
}
tinhSoNgay(ngayBatDau:Date,NgayKetThuc:Date):number{
var timeDiff = Math.abs(ngayBatDau.getTime() - NgayKetThuc.getTime());
var dayDifference = Math.ceil(timeDiff / (1000 * 3600 * 24));
return dayDifference;
}
// Load danh sách hồ sơ chưa giải quyết 2 tháng gần nhất !
loadDanhSachHoSoChuaGiaiQuyetThangHienTai(){
return this.http.get(this.danhSachHoSoChuaGiaiQuyet_Url+“thangBaoCao=”+this.month+"&namBaoCao="+ this.year +"&nguoidungID="+this.GlobalProvider.MaNhanVien+"&sobannganhID="+this.soBanNganhID)
//return this.http.get(this.GlobalProvider.HOST_API_URL +’/api/HoSoChuaGiaiQuyetNguoiDungThangNamBatKy/BaoCaoHoSoChuaGiaiQuyetNguoiDungThangNamBatKy?thangBaoCao=4&namBaoCao=2015&nguoidungID=778&sobannganhID=16’)
.subscribe(
Data =>{
this.danhSachHoSoChuaGiaiQuyetThangHienTai = Data.json();
console.log(this.danhSachHoSoChuaGiaiQuyetThangHienTai);
},err => {
console.log(‘Lỗi = >’+ err);
});
}
loadDanhSachHoSoChuaGiaiQuyetThangTruoc(){
return this.http.get(this.danhSachHoSoChuaGiaiQuyet_Url+“thangBaoCao=”+this.month +"&namBaoCao="+this.year+"&nguoidungID="+this.GlobalProvider.MaNhanVien+"&sobannganhID="+this.soBanNganhID)
//return this.http.get(this.GlobalProvider.HOST_API_URL +’/api/HoSoChuaGiaiQuyetNguoiDungThangNamBatKy/BaoCaoHoSoChuaGiaiQuyetNguoiDungThangNamBatKy?thangBaoCao=4&namBaoCao=2015&nguoidungID=778&sobannganhID=16’)
.subscribe(
Data =>{
this.danhSachHoSoChuaGiaiQuyetThangTruoc = Data.json();
console.log(this.danhSachHoSoChuaGiaiQuyetThangTruoc);
},err => {
console.log(‘Lỗi = >’+ err);
});
}
goManHinhTheoDoiTinhHinh():void{this.navCtrl.push(ManHinhTheoDoiTinhHinhPage);}
goManHinhNguoiDung(){this.navCtrl.push(ThongKeNguoiDungPage);}
goManHinhPhongBan(){this.navCtrl.push(ThongKePhongBanPage);}
goManHinhLinhVuc(){this.navCtrl.push(ThongKeLinhVucPage);}
goManHinhTraCuu(): void {this.navCtrl.push(ManHinhTraCuuPage);}
goManHinhBieuDo(){this.navCtrl.push(BieuDoThongKePage);}
dangXuat():void {this.navCtrl.push(HomePage);}
}

please help me Sir !
Can you teamview in my computer?

I see zero indication that you’re listening to anything I’m saying, so I’m done here.

1 Like

I know how to declare that variable like this
public danhSachHoSoChuaGiaiQuyetThangHienTai:chiTietHoSo[ ] = [ ];
, but it is wrong , not my desire.!