如果我可以將用戶登錄到 Postman,那么這意味著我的前端代碼存在錯誤。對?
CastError:對于值“{電子郵件:'[email protected]',密碼:'123456'}”(型別物件)在模型“用戶”的路徑“電子郵件”中轉換為字串失敗
似乎路徑“電子郵件”同時將電子郵件和密碼輸入到一個輸入中。問題出在我的前端還是后端?我在用react-final-form
減速器:
export const userLoginReducer = (state = {}, action) => {
switch (action.type) {
case USER_LOGIN_REQUEST:
return { loading: true };
case USER_LOGIN_SUCCESS:
return { loading: false, userInfo: action.payload };
case USER_LOGIN_FAIL:
return { loading: false, error: action.payload };
case USER_LOGOUT:
return {};
default:
return state;
}
};
行動:
export const login = (email, password) => async (dispatch) => {
try {
dispatch({
type: USER_LOGIN_REQUEST,
});
const config = {
headers: {
"Content-Type": "application/json",
},
};
const { data } = await axios.post(
"/api/users/login",
{ email, password },
config
);
dispatch({
type: USER_LOGIN_SUCCESS,
payload: data,
});
localStorage.setItem("userInfo", JSON.stringify(data));
} catch (error) {
dispatch({
type: USER_LOGIN_FAIL,
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
});
toast.error("Invalid Credentials");
}
};
店鋪
import {
userLoginReducer,
} from "./reducers/userReducers";
const reducer = combineReducers({
userLogin: userLoginReducer,
});
const userInfoFromStorage = localStorage.getItem("userInfo")
? JSON.parse(localStorage.getItem("userInfo"))
: null;
const initialState = { userLogin: { userInfo: userInfoFromStorage } };
uj5u.com熱心網友回復:
根據錯誤,我懷疑錯誤是由于您的請求正文可能不是字串。
我會嘗試將您的帖子更新為以下內容:
const { data } = await axios.post(
"/api/users/login",
{ JSON.stringify(email), JSON.stringify(password) },
config
);
這將確保您的請求作為字串傳遞到服務器
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380239.html
