可以在 Postman 上登录用户,但不能在浏览器上登录
Can log in user on Postman, but not on browser
如果我可以将用户登录到 Postman,则意味着我的前端代码有错误。对吗?
CastError:模型“用户”的路径“电子邮件”中的值“{电子邮件:'admin@mail.com',密码:'123456'}”(类型对象)转换为字符串失败
似乎路径“email”同时将电子邮件和密码纳入一个输入。问题出在我的前端还是后端?我正在使用 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 } };
根据错误,我怀疑错误是由于您的请求正文可能不是字符串。
我会尝试将您的 post 更新为以下内容:
const { data } = await axios.post(
"/api/users/login",
{ JSON.stringify(email), JSON.stringify(password) },
config
);
这将确保您的请求作为字符串传递到服务器
如果我可以将用户登录到 Postman,则意味着我的前端代码有错误。对吗?
CastError:模型“用户”的路径“电子邮件”中的值“{电子邮件:'admin@mail.com',密码:'123456'}”(类型对象)转换为字符串失败
似乎路径“email”同时将电子邮件和密码纳入一个输入。问题出在我的前端还是后端?我正在使用 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 } };
根据错误,我怀疑错误是由于您的请求正文可能不是字符串。
我会尝试将您的 post 更新为以下内容:
const { data } = await axios.post(
"/api/users/login",
{ JSON.stringify(email), JSON.stringify(password) },
config
);
这将确保您的请求作为字符串传递到服务器