从Firestore查询中获取文档中的用户信息?

问题描述 投票:1回答:3

我正在从Firestore查询一些文档。如何从快照对象获取用户信息。

诸如用户标识和用户名之类的信息。

[假设用户使用社交OAuth提供程序登录。如果重要的话。

firebase.firestore().collection('sample').get()
        .then(function(snapshot) {
            console.log('SNAPSHOT', snapshot);
            snapshot.forEach(function(doc) {
                console.log(doc.exists);
                console.log(doc);
                console.log(doc.id);
                console.log(doc.metadata);
                console.log(doc.ref);
                console.log(doc.data());
                console.log(doc.ref.path);
                console.log(doc);
            })
        }).catch(console.log);
javascript firebase google-cloud-firestore
3个回答
2
投票

Firestore不会将文档与用户关联。如果要将文档与用户相关联,则必须在应用程序代码中执行此操作。您可以在保存用户的每个文档中添加一个字段,在/中使用用户的UID作为文档ID,或将用户的文档存储在子集合中。

如果在包含该用户的每个文档中添加一个字段,则可以通过以下方式获取与该用户关联的所有文档:

firebase.firestore().collection('sample').where('uid', '=', firebase.auth().currentUser.uid).get()...

如果使用用户的UID作为文档ID,则可以通过以下方式获取用户文档:

firebase.firestore().collection('sample').doc(firebase.auth().currentUser.uid)...

如果将用户的文档存储在以该用户命名的文档的子集合中,则可以通过以下方式获得该集合:

firebase.firestore().collection('sample').doc(firebase.auth().currentUser.uid).collection('documents')...

0
投票

正如Frank van Puffelen指出的,您的应用程序需要实现在Firestore / Firebase RTDB上创建用户文档的逻辑。

您要做的工作是:1.)在“身份验证”下的firebase-backend中创建一个用户帐户。您可以提供任何带有密码的电子邮件-这些不是真正的Google帐户,仅用于您的应用程序。

2。)编写登录逻辑,以便您可以使用先前创建的帐户登录到您的应用程序。从服务器中查找onSuccess消息以确保一切正常。

3。)如果用户已登录,则可以从您的应用中检索firebaseAuth.currentUser对象,并获取其他metaData,例如上次登录的时间戳。另外,您现在可以检索他的电子邮件,并可能创建一个唯一的ID。使用此唯一密钥,您现在可以使用自己的个人数据创建新的用户记录。一个典型的结构是:

-您的应用程序名称空间

-用户

[email protected]

---- displayName:frank

----年龄:47

----喜欢吃:意大利面代码

---- [email protected]

...

-数据

...

希望这会有所帮助!


0
投票

您需要使用Firebase管理员SDK编写后端代码。参见https://firebase.google.com/docs/auth/admin/manage-users

admin.auth().getUser(uid)
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log('Successfully fetched user data:', userRecord.toJSON());
  })
  .catch(function(error) {
    console.log('Error fetching user data:', error);
  });
© www.soinside.com 2019 - 2024. All rights reserved.