第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

AXIOS 和 FETCH 在 NODE JS 中沒有出現自定義錯誤

AXIOS 和 FETCH 在 NODE JS 中沒有出現自定義錯誤

狐的傳說 2022-12-29 09:48:45
我正在嘗試捕獲我在后端創(chuàng)建的自定義錯誤,但是 Axios 和 fetch 沒有得到res.status(401).json({msg:"custom error name"}) 我已經從 catch 得到響應但是沒有錯誤沒有消息try {  const loginUser = { email: user.email, password: user.password };  const loginRes = await Axios.post(    "http://localhost:4000/snackhub/login",    loginUser  );  localStorage.setItem("auth-token", loginRes.data.token);  dispatch(fetchUsers());  history.push("/");} catch (err) {  console.log(err.response);  err.response.data.msg && console.log(err.response.data.msg); //hopefully i can display this thhe custom error from the backend}以下是后端錯誤的示例: const { error } = await logInValidation(req.body);if (error) return res.status(400).json({ msg: error.message[0].details }); //is there something wrong in my syntax here?const userExists = await Customer.findOne({ email: email });if (!userExists)  return res.status(400).json({ msg: "email is not registered" });
查看完整描述

4 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

試試這個:


axios.post("http://localhost:4000/snackhub/login",

    loginUser)

.then(res => {

                        

},

error => {


}

);


查看完整回答
反對 回復 2022-12-29
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

對于發(fā)送錯誤或您應該這樣寫的任何響應。

res.status(422).send({ success: false, error: "User already exist!" });


查看完整回答
反對 回復 2022-12-29
?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

中的錯誤處理axios與其他的略有不同。在這里,您在塊中獲取response錯誤對象中的對象。catch我建議您記錄響應,然后相應地進行處理。


一個例子是這樣的:


axios.post(url, data).then(res => {

        // do good things

})

.catch(err => {

    if (err.response) {

      // client received an error response (5xx, 4xx)

    } else if (err.request) {

      // client never received a response, or request never left

    } else {

      // anything else

    }

})


查看完整回答
反對 回復 2022-12-29
?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

我遇到了同樣的問題,我想出了解決這個問題的方法。你可以試試console.log(error.response.data)。這將打印從您的服務器發(fā)送的 JSON 響應...即:res.status({message: "Custom error message"}) 可以在客戶端使用 axios 訪問:


axios.get(url)

.then(response => {

    // your logic 

})

.catch(err => {

    console.log(err.response.data.message)

})


查看完整回答
反對 回復 2022-12-29
  • 4 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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