简体   繁体   English

异步方法总是返回 true

[英]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.

相关问题 Async/await 问题,Async/await function 在值被放入新的 state 之前返回 true - Async / await problem, The Async / await function is returning true before the value is placed in the new state 异步方法 Firebase - Async method Firebase Flutter Firebase authStateChanges 快照始终返回真值,即使用户已删除 - Flutter Firebase authStateChanges Snapshot Always Return True Even User Deleted 返回 null 的 dynamodb 扫描方法 - dynamodb scan method returning null 方法不返回预期结果 flutter - Method not returning expected result flutter 有没有一种方法可以不在异步间隙中使用构建上下文? - Is there a method for not using build context across async gaps? 如何覆盖异步 NDB 方法并编写自己的 tasklet - How to override an async NDB method and write your own tasklet 可读在节点谷歌云存储下载方法中不可异步迭代 - readable is not async iterable in Node Google Cloud Storage download method 谷歌云存储总是返回缓存控制私有 - Google Cloud Storage always returning cache control private Voximplant sendDigits 方法在场景中返回错误 - Voximplant sendDigits method returning error in scenario
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM