简体   繁体   English

未处理的拒绝 (TypeError):无法读取未定义的属性(读取“错误”)

[英]Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'error')

const getToken = (userId, token) => {
  22 |   getmeToken(userId, token).then((info) => {
  23 |     console.log("INFO COMING", info);
> 24 |     if (info.error) {
  25 |      setInfo({ ...info, error: info.error });
  26 |     } else {
  27 |       const clientToken = info.clientToken;

What is meant by Unhandled rejection, cannot read properties of undefined??. Unhandled rejection, cannot read properties of undefined 是什么意思??。

I have defined the error and also assigned a value for it, below is the code of useState我已经定义了错误并为其分配了一个值,下面是useState的代码

  const [info, setInfo] = useState({
    loading: false,
    success: false,
    clientToken: null,
    error:"",
    instance: {},
  });

As per the above code, I have defined and assigned the value for error, then why is the browser still showing cannot read properties of undefined .按照上面的代码,我已经定义并分配了错误的值,那么为什么浏览器仍然显示cannot read properties of undefined Can anyone please clarify this?谁能澄清一下?

getmeToken function code is below getmeToken function 代码如下

export const getmeToken = (userId, token) => {
    return fetch(` ${API}/payent/gettoken/${userId}`, {
        method: "GET",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
            Authorization: `Bearer ${token}`
        }
    }).then(response => {
        return response.json();
    })
    .catch(err => console.log(err))
}

First, check the output of the getmeToken function. Then, you can use info?.error instead of info.error to handle this error.首先查看getmeToken function的getmeToken ,然后可以用info?.error代替info.error来处理这个错误。

Edit : Check the ${API}/payent/gettoken/${userId} url again.编辑:再次检查${API}/payent/gettoken/${userId} url。 I think /payent/ is incorrect ( /payment/ is correct)我认为/payent/不正确( /payment/是正确的)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应:未处理的拒绝(TypeError):无法读取未定义的属性(读取“错误”) - React : Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'error') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“哈希”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'hash') 可能未处理的拒绝 [3] 类型错误:无法读取未定义的属性(读取“原型”) - Potentially unhandled rejection [3] TypeError: Cannot read properties of undefined (reading 'prototype') 未处理的拒绝 (TypeError):无法读取未定义的属性(读取“长度”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'length') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“状态”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'state') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“值”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'value') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“数据”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'data') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“参数”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'params') 未处理的拒绝 (TypeError):无法读取未定义的属性(读取“clientToken”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'clientToken') 错误:'未处理的 Promise 拒绝:TypeError:无法读取未定义的属性 - ERROR: 'Unhandled Promise rejection: TypeError: Cannot read properties of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM