代码之家  ›  专栏  ›  技术社区  ›  Herc

将用户信息添加到firebase(注册)react native

  •  1
  • Herc  · 技术社区  · 3 年前
    createUserWithEmailAndPassword(auth, state.email, state.password)
            .then((userCredential) => {
              setDoc(doc(db, "users", userCredential.user.uid), {
                firstName: state.firstName,
                lastName: state.lastName,
                email: state.email,
                address: state.address,
                username: state.firstName,
              })
    

    1 回复  |  直到 3 年前
        1
  •  0
  •   Obumuneme Nwabude    3 年前

    你在正确的轨道上。你只需要把名字和姓氏连在一起。

    您可以使用+作为可以添加的字符串或使用字符串插值(这更优雅)。

    // adding strings 
    const username = state.firstName + '.' + state.lastName;
    
    // using string interpolation.
    // here, you use the backtick character (`), it is usually found below escape.
    // then use ${variable} to interpolate variable.
    const username = `${state.firstName}.${state.lastName}`;
    

    (because setDoc returns a promise) . 我还通过对象分解使代码更加优雅。

    createUserWithEmailAndPassword(auth, state.email, state.password) 
     .then(async (userCredential) => {
        const { address, email, firstName, lastName } = state;
        const username = `${firstName}.${lastName}`;
        await setDoc(
          doc(db, 'users', userCredential.user.uid), 
          { address, email, firstName, lastName, username },
        );
     });