[英]Async method always returning true
I am implementing a login functionality with firebase and React Native.我正在使用 firebase 和 React Native 实现登录功能。 The
handleLogin
method is always returning succeed()
. handleLogin
方法总是返回succeed()
。 If I remove the success()
, I get:如果我删除
success()
,我得到:
[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'result2.failed')]
[未处理的 promise 拒绝:类型错误:未定义不是 object(评估“result2.failed”)]
export default async function login(credentials) {
let result2= succeed();
result2 = await handleLogin(credentials.email, credentials.password);
if(result2.failed()){
return fail({password: "Login failed"});
}
else{
return succeed();
}
}
const handleLogin = async(email,password) => {
auth.signInWithEmailAndPassword(email, password)
.catch(function(error) {
console.log(error);
return fail("failed");
})
return succeed(); // or fail() in which case method always returns fail()
I have also tried using .then()
but I keep getting object undefined error if I don't return succeed()
or fail()
in the end of handleLogin()
:我也尝试过使用
.then()
但如果我在handleLogin()
末尾不返回succeed()
或fail()
,我会不断收到 object 未定义错误:
auth.signInWithEmailAndPassword(email, password)
.then(function(){return succeed();})
.catch(function(error) {
console.log("we fail"+error);
return fail("failed");
})
Its a known issue of the firebase. Similar thing happened to me on angular code.这是 firebase 的一个已知问题。我在 angular 代码上发生了类似的事情。
I solved the problem by adding a return statement:我通过添加 return 语句解决了这个问题:
return auth.signInWithEmailAndPassword(email, password).then(function(){return succeed();}).catch(function(error) { return fail("Login Attempt Failed\n"+error);})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.