我正在嘗試做一個登錄頁面。登錄后,資料是從 mongodb 獲取的。在我輸入我的憑據到登錄頁面后,它應該將我引導到主頁,但它卻給了我這個錯誤
這是我在瀏覽器中遇到的錯誤。
POST http://localhost:5000/api/auth/login 404 (Not Found)
dispatchXhrRequest @ xhr.js:210
xhrAdapter @ xhr.js:15
dispatchRequest @ dispatchRequest.js:58
request @ Axios.js:108
Axios.<computed> @ Axios.js:140
wrap @ bind.js:9
login @ apiCalls.js:7
handleClick @ Login.jsx:78
callCallback @ react-dom.development.js:3945
invokeGuardedCallbackDev @ react-dom.development.js:3994
invokeGuardedCallback @ react-dom.development.js:4056
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:4070
executeDispatch @ react-dom.development.js:8243
processDispatchQueueItemsInOrder @ react-dom.development.js:8275
processDispatchQueue @ react-dom.development.js:8288
dispatchEventsForPlugins @ react-dom.development.js:8299
(anonymous) @ react-dom.development.js:8508
batchedEventUpdates$1 @ react-dom.development.js:22396
batchedEventUpdates @ react-dom.development.js:3745
dispatchEventForPluginEventSystem @ react-dom.development.js:8507
attemptToDispatchEvent @ react-dom.development.js:6005
dispatchEvent @ react-dom.development.js:5924
unstable_runWithPriority @ scheduler.development.js:468
runWithPriority$1 @ react-dom.development.js:11276
discreteUpdates$1 @ react-dom.development.js:22413
discreteUpdates @ react-dom.development.js:3756
dispatchDiscreteEvent @ react-dom.development.js:5889
這是我的路由器檔案。
//LOGIN
router.post('/login', async (req, res) => {
try{
const user = await User.findOne(
{
userName: req.body.user_name
}
);
!user && res.status(401).json("Wrong User Name");
const originalPassword = user.password
const inputPassword = req.body.password;
originalPassword != inputPassword &&
res.status(401).json("Wrong Password");
const { password, ...others } = user._doc;
;
}catch(err){
res.status(500).json(err);
}
});
這是我的 index.js 檔案。
app.use("/api/authentication" , authenticationRoute);
這是我的前端登錄頁面部分。
const Login = () => {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const dispatch = useDispatch();
const { isFetching, error} = useSelector((state) => state.user);
const handleClick =(e) =>{
e.preventDefault();
login(dispatch, {username,password});
}
前端登錄頁面標簽部分基本是這樣的
placeholder = "Username"
onChange={(e)=> setUsername(e.target.value)}/>
<Input
placeholder = "Password"
type = "password"
onChange={(e)=> setPassword(e.target.value)}/>
<Button onClick={handleClick} disabled={isFetching}>LOGIN </Button>
{error && <Error>Something went wrong!</Error>}
uj5u.com熱心網友回復:
在您的后端,您已經宣告了您的登錄路線 http://localhost:5000/api/authentication/login,但在您呼叫的前端,http://localhost:5000/api/auth/login 您就擁有了404 Not Found。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/412405.html
標籤:
