如何在回邮中使用特定回复

我在服务器上发了一个帖子,可以返回res {成功:true}

enter image description here

在我的帖子中,如果收到此回复,则表示我正在努力

this.successPost = true;

this.invitePost = false;

仅对这种确切的回应。 res {成功:正确}

我的ts代码始终在任何res响应上返回if,而永不返回else。

ts

async sendInvite() {
  let formData = {
    "firstName" : this.data.firstName,
    "lastName" : this.data.lastName,
    "email" : this.data.email
  };
  this.httpClient.post<any>('https://8618' + formData, { 
    headers: { 'x-authorization-token': 'XXXXXXXXXX' }}   
    )
    .pipe(
      finalize(() => { })
    ).subscribe(
        res => {
          console.log ("res", res);
          let response = JSON.stringify(res);
            console.log ("response", response);
            if (res = {success: true} ) {
                this.presentToast(response);
                this.successPost = true;
                this.invitePost = false;
              } else {
                this.presentToast(response);
              }
        },
        (err: HttpErrorResponse) => {  // fire on offline
          console.log("err.error", err.error);
          this.presentToast(err.error);
        }  
  );
}
评论
  • 红颜暗
    红颜暗 回复

    我建议您不要对响应对象进行字符串化处理,而应按原样使用它。并使用相等运算符(===)代替赋值运算符。

        async sendInvite() {
      let formData = {
        "firstName" : this.data.firstName,
        "lastName" : this.data.lastName,
        "email" : this.data.email
      };
      this.httpClient.post<any>('https://8618' + formData, { 
        headers: { 'x-authorization-token': 'XXXXXXXXXX' }}   
        )
        .pipe(
          finalize(() => { })
        ).subscribe(
            response => {
              console.log ("response ", response );
                if (res.success === true) {
                    this.presentToast(response);
                    this.successPost = true;
                    this.invitePost = false;
                  } else {
                    this.presentToast(response);
                  }
            },
            (err: HttpErrorResponse) => {  // fire on offline
              console.log("err.error", err.error);
              this.presentToast(err.error);
            }  
      );
    }
    
  • 上集回顾
    上集回顾 回复

    那是因为您要在if条件中分配值。

    Changing if (res = {success: true} ) to if (res === {success: true} ) should do for you. But then you might face another problem because you are stringifying the response.

    Your final if condition should be like if (res === '{"success": true}' )

    希望能帮助到你。