提问者:小点点

如何在axios[REACT,EXPRESS,NODE JS]中访问403的响应规约


因此,在我的组件中,我有一个register函数,它调用我的api,并且在api中,我检查电子邮件是否存在,并且根据发送适当的状态,statusText在这里是我的express应用程序中的api调用处理程序:


router.post('/register',async(req,res)=>{
    const {email,password,firstname,lastname}=  req.body
    try {
        const passHash=await bcrypt.hash(password,1);
        if(passHash == undefined) throw new Error('erro hassing password'); 
        
        const checkEmailExistsPromise= await UserModel.findOne({email:email})
        if( checkEmailExistsPromise !== null) throw new Error('EMAIL')

        res.statusMessage = "user registerd ";
        res.sendStatus(200);
    } catch (error) {
        if(error.message="EMAIL"){
             res.statusMessage='email already used';
             res.sendStatus(403)
            }
            console.log(error)
    }
})

这是我的react组件中的寄存器函数thjat

    const register=async (e)=>{
         e.preventDefault()
         if(!validate()) return ; 
         try {
             const regsterUserPromise= await axios.post('http://localhost:4000/users/register',{...userInfo})
             //when its a 200 status ts easy 
             console.log(regsterUserPromise.statusText)
         } catch (error) {
             //I want to get the status text of that 403 response 
             console.log(error)
         }
    }
now all I'm getting is 
[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/v3Rfb.png

共1个答案

匿名用户

因此要访问状态文本,我所要做的就是在catch块中的error.response.statusText