【问题标题】:Cant pass uid from created user firebase无法从创建的用户 firebase 传递 uid
【发布时间】:2021-07-02 13:43:24
【问题描述】:

我无法传递从 firebase 云函数创建的新创建用户的 uid。用户已成功创建,但 firestore 集合中的文档未成功创建。当我像这样从.doc() 中删除参数时,用户被创建但随机生成的uid 不是从创建用户ofc 创建的。我想要的是从创建的用户传递 uid 作为文档名称。 uid 可从createUsr 函数的响应中获得。

我认为我在使用异步函数的方式上做得不对。

const handleSubmit = async (values) => {
    const createUsr = functions.httpsCallable('createUsr')

    try {
        await createUsr({ email: values.email }).then((data) => {
            const id = data.uid
            const newDocRef = db.collection('students').doc(id)
            newDocRef.set({
              userID: id,
              firstName: values.firstName,
              lastName: values.lastName,
              access_type:  "student"
            })
        })
        success()
    } catch {
        error()
    }
  }

来自控制台的数据响应

Object
  data:
    disabled: false
    displayName: null
    email: "test@gmail.com"
    emailVerified: false
    metadata: {creationTime: "Tue, 06 Apr 2021 19:06:19 GMT", lastSignInTime: null, lastRefreshTime: null}
    passwordHash: null
    passwordSalt: null
    phoneNumber: null
    photoURL: null
    providerData: []
    tenantId: null
    tokensValidAfterTime: "Tue, 06 Apr 2021 19:06:19 GMT"
    uid: "GHpc2ykVj3RPnMqPDDiDiViVvmc2"
  __proto__: Object
  __proto__: Object

【问题讨论】:

  • 尝试删除等待。
  • @KToxcon 它现在可以工作,但它会从随机生成的 uid 中创建用户,例如 data.uid 从未通过
  • 能否在控制台中记录数据并分享给我
  • @KToxcon 问题已更新为来自 concole 的回复

标签: reactjs firebase google-cloud-firestore firebase-authentication


【解决方案1】:

试试这个:

const handleSubmit = async (values) => {
   const createUsr = functions.httpsCallable('createUsr')

   try {
    createUsr({ email: values.email }).then(({ data }) => {
        const { uid } = data;
        const newDocRef = db.collection('students').doc(uid);

        await newDocRef.set({
          userID: uid,          
          access_type:  "student",
          ...values
        });
    });

    success();
  } catch {
    error()
  }
}

【讨论】:

  • 谢谢这个作品,我看到了解决方案一旦添加了对问题的回复......不知道我是怎么错过的。感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 2015-01-18
  • 2018-08-14
  • 2020-03-20
  • 2020-08-26
  • 2019-05-28
  • 1970-01-01
  • 2020-10-26
  • 2021-08-04
相关资源
最近更新 更多