我已经建立了用户登录功能:
export const userLogin = (email, password) => (dispatch) => {
console.log(email, password);
dispatch({ type: actionTypes.AUTH_LOGIN_STARTED });
console.log("after dispatch");
Parse.User.logIn(email, password, {
success(user) {
console.log("in success");
dispatch({
type: actionTypes.AUTH_LOGIN_SUCCESS,
user: user.toJSON(),
});
window.location.replace('/');
},
error(user, error) {
console.log("in error")
console.log({ error });
// The login failed. Check error to see why.
dispatch({
type: actionTypes.AUTH_LOGIN_ERROR,
error,
});
},
});
};
,但是它总是在Parse.User.logIn
之后卡住,它不会成功也不会出错。我已经记录了电子邮件和密码,它们是正确的。
那我在这里想念什么?
答案 0 :(得分:1)
Parse.User.Login
does not have any third parameter。您应该使用promise functions:
Parse.User.logIn(email, password)
.then((user) => {
console.log("in success");
dispatch({
type: actionTypes.AUTH_LOGIN_SUCCESS,
user: user.toJSON(),
});
window.location.replace('/');
})
.error((user, error) => {
console.log("in error")
console.log({ error });
// The login failed. Check error to see why.
dispatch({
type: actionTypes.AUTH_LOGIN_ERROR,
error,
});
});
或者,如果您足够看中,可以使用新的await
语法(我认为这样更简洁):
export const userLogin = (email, password) => async (dispatch) => {
console.log(email, password);
dispatch({ type: actionTypes.AUTH_LOGIN_STARTED });
console.log("after dispatch");
try {
const user = await Parse.User.logIn(email, password);
console.log("in success");
dispatch({
type: actionTypes.AUTH_LOGIN_SUCCESS,
user: user.toJSON(),
});
window.location.replace('/');
} catch (error) {
console.log("in error")
console.log({ error });
// The login failed. Check error to see why.
dispatch({
type: actionTypes.AUTH_LOGIN_ERROR,
error,
});
}
};