我的快速API终点在这里:
@app.post("/api/signup", status_code=status.HTTP_200_OK)
async def signup( credentials : signupcred ):
try:
print(credentials.email, credentials.password1, credentials.name )
response = account.create(email= credentials.email, password= credentials.password1, name = credentials.name , user_id= ID.unique() )
return {response}
except Exception as e:
raise HTTPException(status_code= status.HTTP_403_FORBIDDEN) # Return an appropriate error status code
在前端,我想在引发异常后打印响应,因为响应很好地解释了错误.我不想向用户发送http错误代码.
我的前端是:
const handlesignup = async (e: any) => {
e.preventDefault();
setLoading((loading) => !loading);
try {
const signupresponse = await axios.post("/api/signup", {
email: signupemail,
password1: password1,
name: name,
}); // Send as query paramers );
// const loginresponse = await axios.post('/api/login', {email, password} )// Send as query paramers );
// setIsloggedin(()=> true)
router.replace("/dashboard");
setLoading((loading) => !loading);
} catch (error) {
alert(error);
}
};
console.log不起作用,因为我使用具有快速API后台的nextjs.我只是想将创建函数的响应打印到前端,以便用户知道为什么发生函数错误.此时alert 显示:Axios错误:请求失败,状态代码403