猿问

firebase/firestore 将用户保存到集合

我正在尝试将用户保存到我的 Firestore 中的集合中。似乎正在创建用户,我可以在Authentication选项卡中看到它们,但是它们没有保存到我的 Firestore 内的收藏中。我的控制台内似乎也没有任何错误。我已经为此苦苦挣扎了几个小时,我什至不知道如何调试它。


export const authMethods = {

  signup: (email, password, setErrors, setToken) => {

    firebase

      .auth()

      .createUserWithEmailAndPassword(email, password)

      // make res asynchronous so that we can make grab the token before saving it.

      .then(async res => {

        const token = await Object.entries(res.user)[5][1].b

        // set token to localStorage

        await localStorage.setItem('token', token)

        // grab token from local storage and set to state.

        setToken(window.localStorage.token)

        const userUid = firebase.auth().currentUser.uid

        const db = firebase.firestore()

        db.collection('/users')

          .doc(userUid)

          .set({

            email,

            password,

          })

        console.log(res)

      })

      .catch(err => {

        setErrors(prev => [...prev, err.message])

      })

  },

 ....

}

有任何想法吗?


皈依舞
浏览 120回答 1
1回答

肥皂起泡泡

await从中删除localStorage.setItem不是异步函数。您还需要将 await 添加到db.collection("/users").doc(userUid)这是您可以做的另一种方法。让云功能为您处理。每当创建用户时,都会触发以下功能。export const onUserCreate = functions.auth  .user()  .onCreate(async (user, context) => {    await admin.firestore().collection("users").doc(user.uid).set({      id: user.uid,      emailAddress: user.email,      verified: user.emailVerified,    });  });如果您需要有关云功能的更多信息,请阅读以下内容。https://firebase.google.com/docs/functions/get-started
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答