[英]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.