简体   繁体   English

来自Promise React Native的结果

[英]Result from promise React Native

Here is me connexion method: 这是我的连接方法:

export  function connexionUser(email, password) {

    result = firebase.auth().signInWithEmailAndPassword(email, password)
        .then((res) => {
            console.log("res: ", res)
            return true;
        })
        .catch(function (error) {
            return false;
        });
        return result;
};

Here my method in index.js: 这是我在index.js中的方法:

login = () => {

    const { email, password } = this.state;
    var result = connexionUser(email, password);
    console.log("result");
    console.log(result);
    if (result == true) {
        this.props.navigation.navigate('Favorites');
    }
}

Problem : I would like to get the result of "connexionUser()" in my index.js to be able to manage the navigation or something else. 问题:我想在index.js中获得“ connexionUser()”的结果,以便能够管理导航或其他操作。 But my result isn't true or false: 但是我的结果不是对还是错:

result
A {
  "a": 0,
  "b": null,
  "c": A {
    "a": 0,
    "b": qb {
      "a": [Circular],
      "b": [Function anonymous],
      "c": false,
      "f": null,
      "g": [Function anonymous],
      "next": null,
    },
    "c": A {
      "a": 0,
      "b": qb {
        .....
        .....and more

PS : i am a beginner in React native. PS:我是React Native的初学者。

You can always try to throw in a console.log to see what the output of the result is. 您始终可以尝试在console.log中输入结果,以查看结果。 If you're having trouble debugging with React Native, the docs on debugging are really helpful. 如果您在使用React Native 进行调试时遇到麻烦, 那么有关调试的文档将非常有帮助。

As it stands, your registerUser will return undefined since you aren't actually returning the result. 就目前而言,您的registerUser将返回undefined因为您实际上并未返回结果。 I imagine you're trying to return result in your then statement, but the then statement should be receiving the results of your firebase function and returning those results. 我想您正在尝试在then语句中返回result ,但是then语句应该正在接收firebase函数的结果并返回这些结果。 I'd recommend reading up a bit about Promises for clarification on how they work. 我建议您阅读一些有关Promises的内容,以阐明其工作方式。 One way you could rewrite this would be: 您可以重写的一种方法是:

export function registerUser(email, password) {

    result = firebase.auth().createUserWithEmailAndPassword(email, password).then((res) => {
            console.log("res: ", res)
            return res;
        })
        .catch((error) => {
            var errorCode = error.code;
            var errorMessage = error.message;
            alert(errorMessage);
        });
    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM