Angular HttpClient向后端发送多个请求

时间:2018-02-27 17:37:07

标签: angular firebase-authentication httpclient angularfire2

我的代码似乎是向服务器发送多个请求并缓存先前发送的请求。不太清楚为什么会这样,但这里是代码 -

async onSubmit() {

    let formData =
      {
        firstName: this.firstName,
        lastName: this.lastName,
        gender: this.gender,
        dateOfBirth: this.dateOfBirth
      };

    let url = 'ip';

    this.afAuth.authState.subscribe(data => {
      if (data && data.email && data.getIdToken(true).then((idToken) => {

        const httpOptions = {
          headers: new HttpHeaders({
            'Content-Type': 'application/json',
            'Authorization': idToken
          })
        };

        this.http.post(url, formData, httpOptions)
          .subscribe(data => {
            console.log(data;
          }, error => {
            console.log(error);// Error getting the data
          });

      }))
        window.alert("You have been registered.");
    })

  }

任何人都知道这里发生了什么?

1 个答案:

答案 0 :(得分:0)

修正了它。它发送了多个请求,因为它是异步功能。删除async关键字修复了它。