【问题标题】:Angular HttpClient sending multiple requests to backendAngular HttpClient 向后端发送多个请求
【发布时间】:2018-08-07 10:58:41
【问题描述】:

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

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.");
    })

  }

有人知道这里发生了什么吗?

【问题讨论】:

  • 很难说没有minimal reproducible examplethis.afAuth.authState 是否发出多个值?每一个都会触发一个新的请求。还是onSubmit 被多次调用?尝试输入一些console.logs 看看它会到达哪里。

标签: angular firebase-authentication httpclient angularfire2


【解决方案1】:

修复它。它发送多个请求,因为它是一个异步函数。删除 async 关键字修复了它。

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2019-11-10
    • 2023-04-05
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多