如何获取数据并根据另一个api调用的数据调用api

问题描述 投票:0回答:1

我试图调用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
angular angular4-httpclient
1个回答
1
投票

在方法getAvailableSabhaAttendance中:

  1. 你已经有一个块(命名数据)来处理返回的数据,你可以包含2个以上的块
  2. 错误块(在下面命名为errr):它可以处理从服务方法返回的错误,如果你愿意,可以调用你的'第二个函数'
  3. finally block,(写为()=> {}如下所示:这是您可以验证第一个块中收到的数据的地方,如果出现验证问题,您可以调用“第二个函数”

    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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.