未处理的拒绝 (TypeError):无法读取未定义的属性(读取 'error')

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;

未处理的拒绝是什么意思,无法读取未定义的属性??。

我已经定义了error并且给它赋值了,下面是useState

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

按照上面的代码,我已经定义并赋值了error,为什么浏览器还是显示无法读取未定义的属性。 谁能澄清一下?

getmeToken函数代码如下

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))
}

首先,检查 getmeToken 函数的输出。然后,您可以使用 info?.error 而不是 info.error 来处理此错误。

编辑: 再次检查 ${API}/payent/gettoken/${userId} url。我认为 /payent/ 不正确(/payment/ 正确)