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

UVi*_*Vic 2 javascript firebase google-cloud-firestore

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

用户 ID 和用户名等信息。

假设用户使用社交 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);
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 5

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

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

firebase.firestore().collection('sample').where('uid', '=', firebase.auth().currentUser.uid).get()...
Run Code Online (Sandbox Code Playgroud)

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

firebase.firestore().collection('sample').doc(firebase.auth().currentUser.uid)...
Run Code Online (Sandbox Code Playgroud)

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

firebase.firestore().collection('sample').doc(firebase.auth().currentUser.uid).collection('documents')...
Run Code Online (Sandbox Code Playgroud)