为了账号安全,请及时绑定邮箱和手机立即绑定

firebase/firestore 将用户保存到集合

firebase/firestore 将用户保存到集合

皈依舞 2022-10-13 19:29:29
我正在尝试将用户保存到我的 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])      })  }, ....}有任何想法吗?
查看完整描述

1 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

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


查看完整回答
反对 回复 2022-10-13
  • 1 回答
  • 0 关注
  • 119 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号