【问题标题】:How To Read / Parse Firebase Response .createUserWithEmailAndPassword如何阅读/解析 Firebase 响应 .createUserWithEmailAndPassword
【发布时间】:2020-02-12 13:15:18
【问题描述】:

我收到了来自 firebase 的回复,但不确定如何检索它。当我 console.log() 得到响应时,我得到 [object Object] 而当我 JSON.stringify() 得到响应时,我得到的东西不太有用{"_40":0,"_65":0,"_55":null,"_72":null}

我可以在终端中看到来自 firebase 的回复:Possible Unhandled Promise Rejection (id: 0):[Error: The email address is already in use by another account.]. 这很有帮助,但我不知道如何将其放入我的代码中。

const signUp  = (dispatch) => {
    return  async ({userData})=>{
        try{
            const user = {
                userName: 'test4@test.com',
                password: 'password4'
            };

            //creates user with firebase API
            const response = config.createUser(
                user
            );
            await AsyncStorage.setItem('token', response.__________); 
            dispatch({type: 'sign_up', payload: response.__________});
        } catch(e){
            dispatch({type: 'add_error', payload: 'Something went wrong with sign up'});
        }
    };
};

返回的对象中包含令牌,我尝试了多种方法来获取response.data.apiKeyresponse.data.stsTokenManager.accessToken....但没有运气。

Object {
  "apiKey": "Aaskjdfj;lkar9V0",
  "appName": "[DEFAULT]",
  "authDomain": "test.firebaseapp.com",
  "createdAt": "1571092641959",
  "displayName": null,
  "email": "test6@test.com",
  "emailVerified": false,
  "isAnonymous": false,
  "lastLoginAt": "1571092641959",
  "phoneNumber": null,
  "photoURL": null,
  "providerData": Array [
    Object {
      "displayName": null,
      "email": "test6@test.com",
      "phoneNumber": null,
      "photoURL": null,
      "providerId": "password",
      "uid": "test6@test.com",
    },
  ],
  "redirectEventId": null,
  "stsTokenManager": Object {
    "accessToken": "eyas;lkdjf;lakfj4TQ",
    "apiKey": "AIzlaskjdf;klajsdfklfnXr9V0",
    "expirationTime": 1571170025283,
    "refreshToken": "AEusjkdfkl;ajsf;kljawSg",
  },
  "tenantId": null,
  "uid": "zU4rhnTnKoNGLlu3gctP2zLorhB2",
}

config.js 文件:

createUser = async (user) => {
    console.log('user.email and pw: ' + user.userName + user.password);
    await firebase
    .auth()
    .createUserWithEmailAndPassword(user.userName, user.password)
    .then(console.log(firebase.auth().currentUser));
};

【问题讨论】:

  • 请编辑问题以显示实际调用 createUserWithEmailAndPassword 的代码。我希望看到一个处理它返回的承诺的调用。
  • @DougStevenson 我添加了调用 createUserWithEmailAndPassword 的配置文件

标签: json react-native parsing react-redux firebase-authentication


【解决方案1】:

在我看来,您可能希望从createUser 返回由createUserWithEmailAndPassword 产生的UserCredential 对象。

createUser = async (user) => {
    console.log('user.email and pw: ' + user.userName + user.password);
    return await firebase.auth()
        .createUserWithEmailAndPassword(user.userName, user.password)
};

然后你可以在调用函数中使用它:

try {
    const userCrediential = await config.createUser(user);
}
catch (error) {
    // deal with the error here if the user account already exists
}

并使用您在文档中看到的属性做任何您想做的事情。 catch 块将从被拒绝的 Promise 中传递错误,以便您知道下一步该做什么。

【讨论】:

  • 我也错过了 await 关键字,因此在响应返回之前 console.logs 正在运行。谢谢,这很有帮助!
【解决方案2】:

除了 Dougs 的回答之外,标注后不能有 .then() 。看来函数完成后只能有一个回调。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 2019-09-05
    相关资源
    最近更新 更多