如何向react-firebase添加自定义注册字段

问题描述 投票:0回答:1

大家好我试图添加一个自定义用户字段作出反应,我试图在db中使用已注册的用户ID制作单独的用户表。然而我收到一个错误,说user.uid由于某种原因未定义。谁能告诉我我做错了什么?

import React, { Component } from "react";
import SignUpView from "./SignUpView";
import { withRouter } from "react-router";
import app from "../config/dev";
import { UserRef, timeRef } from '../components/reference';
class SignUpContainer extends Component {
  handleSignUp = async event => {
    event.preventDefault();
    const { email, password,rank } = event.target.elements;


    try {
      const user = await app
        .auth()
        .createUserWithEmailAndPassword(email.value, password.value);
        console.log(user.email);
        UserRef.child(user.uid).set(rank.value);
        alert("you have succseffully signed up!!")
      this.props.history.push("/");
    } catch (error) {
      alert(error);
    }
  };

  render() {
    return <SignUpView onSubmit={this.handleSignUp} />;
  }
}

export default withRouter(SignUpContainer);
reactjs firebase
1个回答
0
投票

createUserWithEmailAndPassword()方法返回一个Promise<firebase.auth.UserCredential>对象。 uid财产嵌套在firebase.auth.UserCredential.user财产内。

请尝试此更新的代码:

try {
  const userCredential = await app
    .auth()
    .createUserWithEmailAndPassword(email.value, password.value);

    console.log(userCredential.user.email);
    UserRef.child(userCredential.user.uid).set(rank.value);

    alert("you have succseffully signed up!!")
  this.props.history.push("/");
} catch (error) {
  alert(error);
}
© www.soinside.com 2019 - 2024. All rights reserved.