【问题标题】:Firestore user id is different of the document id react native firebaseFirestore 用户 ID 与文档 ID 不同,反应原生 Firebase
【发布时间】:2021-02-09 14:21:27
【问题描述】:

我正在做一个简单的用户创建,但是firestore中的id字段为null并且id不同。

为什么会这样?如何解决?

代码如下:

const auth = await authFB().createUserWithEmailAndPassword(data.email, data.password);

const db = firestore();

await db
        .collection('Clients')
        .doc(auth.user.uid)
        .set({
          id: auth.user.uid,
          name: {
            first: firstName,
            last: lastName
            }
         });

文档图片如下:(看id:null):

【问题讨论】:

  • 是用户在 Auth 数据库中创建的吗?也许 authFB().createUserWithEmailAndPassword 会抛出错误。试着抓住它并检查
  • @SergeiSevriugin 我是这样使用的:import authFB from '@react-native-firebase/auth';
  • @SergeiSevriugin 它没有返回错误只是正常注册用户
  • 我不明白,在您的代码中,您为文档设置了 id 和 name 属性,文档等于 auth.user.uid?那么当你创建这个文档时,如果你刚刚从 createUserWithEmailAndPassword 得到这个 id 呢?如果您使用 auth.user.uid 正确设置了名称名称属性,那么未设置 id 吗?
  • @SergeiSevriugin 是的,这是我的问题,id 为空,如果我创建 console.log (user.uid),uid 与 firestore 中的文档 id 不同

标签: javascript firebase react-native google-cloud-firestore react-native-firebase


【解决方案1】:

解决方案是创建const uid = auth.user.uid 并在firestore 更新中分两步使用此常量。例如:

const auth = await authFB().createUserWithEmailAndPassword(data.email, data.password);

const uid = auth.user.uid;      // <----

const db = firestore();

await db
        .collection('Clients')
        .doc(uid)              // <----
        .set({
          id: uid,             // <----
          name: {
            first: firstName,
            last: lastName
            }
         });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-29
    • 2020-06-10
    • 2019-11-15
    • 2018-07-06
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 2021-11-07
    相关资源
    最近更新 更多