TypeError: 无法读取未定义的属性(读取 ‘api_url’)

huangapple go评论77阅读模式
英文:

TypeError: Cannot read properties of undefined (reading 'api_url')

问题

我正在尝试通过用户的电子邮件地址验证用户是否存在,以便使用AngularJS重置密码。



private api_url = AppSettings.API_ENDPOINT + 'user-info/email_validate';

......

  async function sendRequest<T>(email:string):Promise<T>{
      // console.log(this.api_url);
      const res = await fetch (this.api_url, {
        method:"post",
        headers: {"Content-Type": "application/json"},
        body: JSON.stringify({
          email: email,
        })
      }).then((res) => {
        if(!res.ok){
          console.log("ERROR");
          return res;
        }
        return res;
      })

      return res.json();
    }

尝试后,我的文件输出如下:

ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'api_url')

TypeError: Cannot read properties of undefined (reading 'api_url')

不知道该怎么办。

英文:

I am trying to validate whether the user exists via his email in order to reset password with AngularJS.

  
private api_url = AppSettings.API_ENDPOINT + &#39;user-info/email_validate&#39;;

......



  async function sendRequest&lt;T&gt;(email:string):Promise&lt;T&gt;{
      // console.log(this.api_url);
      const res = await fetch (this.api_url, {
        method:&quot;post&quot;,
        headers: {&quot;Content-Type&quot;: &quot;application/json&quot;},
        body: JSON.stringify({
          email: email,
        })
      }).then((res) =&gt; {
        if(!res.ok){
          console.log(&quot;ERROR&quot;);
          return res;
        }
        return res;
      })

      return res.json();
    }

After trying this, my file outputs are:

ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading &#39;api_url&#39;)

TypeError: Cannot read properties of undefined (reading &#39;api_url&#39;)

Don't know what to do.

答案1

得分: 1

你无法在典型的函数中获取this。你可以使用箭头函数来解决这个问题。

class Demo {
  private api_url = `${AppSettings.API_ENDPOINT}user-info/email_validate`;

  // ....

  sendRequest = async <T>(email: string): Promise<T> => {
    // console.log(this.api_url);
    const res = await fetch(this.api_url, {
      body: JSON.stringify({
        email,
      }),
      headers: { 'Content-Type': 'application/json' },
      method: 'post',
    }).then((res) => {
      if (!res.ok) {
        console.log('ERROR');

        return res;
      }

      return res;
    });

    return res.json();
  };
}

更多信息请参考
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#this_in_arrow_functions

英文:

You can't get this in a typical function. You can use the arrow function to solve it.

class Demo {
  private api_url = `${AppSettings.API_ENDPOINT}user-info/email_validate`;

  // ....

  sendRequest = async &lt;T&gt;(email: string): Promise&lt;T&gt; =&gt; {
    // console.log(this.api_url);
    const res = await fetch(this.api_url, {
      body: JSON.stringify({
        email,
      }),
      headers: { &#39;Content-Type&#39;: &#39;application/json&#39; },
      method: &#39;post&#39;,
    }).then((res) =&gt; {
      if (!res.ok) {
        console.log(&#39;ERROR&#39;);

        return res;
      }

      return res;
    });

    return res.json();
  };
}

For more information
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#this_in_arrow_functions

huangapple
  • 本文由 发表于 2023年1月6日 14:28:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定