亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React - Django:TypeError:無法讀取未定義的屬性“令牌”

React - Django:TypeError:無法讀取未定義的屬性“令牌”

弒天下 2023-06-09 15:17:14
我正在嘗試使用 react 和 django rest 登錄。我有登錄后端的 rest-auth,用戶來自 LDAP。django 上的登錄有效。當從 React 調用時,我的后端的響應也有效。我嘗試使用 react-cookie 將我的令牌保存在 cookie 中。但是當我這樣做時,出現錯誤:TypeError: Cannot read property 'token' of undefined我拆分了我的代碼。我有一個文件 api_auth_service.jsexport class APILogin {    static loginUser(body){        return fetch('http://127.0.0.1:8000/rest-auth/login/', {            method: 'POST',            headers: {                'Content-Type': 'application/json'            },            body: JSON.stringify(body)        }).then( resp => resp.json())    }}和我的登錄視圖反應:export default function Login() {  const [ username, setUsername] = useState('');  const [ password, setPassword] = useState('');  const [token, setToken] = useCookies(['gra-token']);  useEffect(() => {    console.log(token);  }, [token])  const loginClicked = () => {    APILogin.loginUser({username, password})    .then( resp => console.log(resp))    .then(resp => setToken('gra-token', resp.token))    .catch( error => console.log(error))  }  return ( .............正如您所看到的,保存到我的 cookie 中的顯然不是令牌
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

const loginClicked = () => {

 APILogin.loginUser({username, password})

 .then( resp => resp)

 .then(resp => setToken('gra-token', resp.token))

 .catch( error => console.log(error))

}

您需要從第一個 then 語句返回 resp


查看完整回答
反對 回復 2023-06-09
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號