首页 > 解决方案 > React-Native firebase 注册我如何获取用户名

问题描述

这是我的注册码;

onButtonClicked1() {
  this.setState({
    error: '',
    loading: true
  })
  const {username, password} = this.state;
  firebase.auth().createUserWithEmailAndPassword(username, password)
  .then(this.onRegisterSuccess.bind(this))
  .catch(() => {
    this.setState({
      error : 'Not registered.',
      loading: false
    })
  });
}

我如何获取用户名?获取用户名后,可以在设置页面进行更新。

标签: firebasereact-native

解决方案


在 promise 解决后,createUserWithEmailAndPasswordfirebase 方法将返回一个具有此结构的 Object(将来可能会更改):

{
  additionalUserInfo: Object,
  credential: null,
  operationType: "signIn",
  user: {
    displayName: string,
    email: string,
    emailVerified: string,
    uid: string,
    ...
  }
}

从那里您可以访问所有可用信息。但是,请记住,这createUserWithEmailAndPassword是期待emailpassword。因此,如果username您的应用程序中是电子邮件,那么一切都很好。如果没有,您可能无法获得您想要的结果。

解决 promise 后使用函数的示例:

firebase.auth().createUserWithEmailAndPassword(username, password)
  .then((res) => {
    callAnyFunctionYouWant(res.user.email) // or res.user.displayName
  })
  .catch(() => {
    ...
  });

如果您确实想要用户名

如果您有username要使用的自定义,您需要在之后立即为用户创建一个配置文件,firebase.auth()并使用不同的查询来获取它。那,你不能用你提供的示例代码来做。它需要额外的工作。

希望这能说明问题。


推荐阅读