我试图调用api,如果返回任何数据,那么我必须显示其他数据,我将调用另一个api来获取数据。
我有服务,可以获取数据给我。如果有任何数据,那么我将使用该数据来呈现html文件。但是,如果没有数据,那么我必须调用另一个API来获取数据。
我在构造函数中调用函数。
this.availableDetailsArray = this.getConstactListBySabhaID(this.route.snapshot.params['id']);
上面的代码调用了这个函数。
getAvailableSabhaAttendance(id) {
this.contactService.getAttendanceIfAvailable(id).subscribe(data => {
let temp = [];
temp = data.data;
this.availableSabhaDetails = temp;
console.log(this.availableSabhaDetails);
});
return this.availableSabhaDetails
}
如果上面的api中有数据,那我很好。但是,我无法将此数据称为另一个api。
第二个函数返回:
{id: 12, topic_name: "Nishtha", sabha_id: 8, sabha_child_id: 1, speaker_name1: 56, …}
absent_contact: "129"
attendance_id: 10
created_at: "2018-12-27 22:13:31"
id: 12
present_contact: "55,118,122,126,116,58,125,119,132"
sabha_child_id: 1
sabha_date_time: "2018/12/18 - 13:45"
sabha_details: "Video Darshan"
sabha_id: 8
speaker_name1: 56
speaker_name2: 18
topic_name: "Nishtha"
updated_at: "2019-01-13 14:50:42"
__proto__: Object
在方法getAvailableSabhaAttendance中:
getAvailableSabhaAttendance(id) {
this.contactService.getAttendanceIfAvailable(id).subscribe(
data => {
let temp = [];
temp = data.data;
this.availableSabhaDetails = temp;
console.log(this.availableSabhaDetails);
}
,errr => { /* This is the case of error, you can call the 2nd API here */}
,() => {
/* This is the case of finally, you can validate the data here and if the validations fail, call the 2nd API here */
}
);
return this.availableSabhaDetails;
}