Cloud Firestore,检索当前用户

问题描述 投票:0回答:2

除了正确的方法之外,我已尝试从Cloud Firestore中的用户集合中检索currentUser(doc)。

比如vue-firestore:

这个。$ binding(“user”,firebase.firestore()。collection(“users”)。doc(this.firebase.auth()。currentUser))

非常感谢你给我的任何帮助。

firebase-authentication google-cloud-firestore
2个回答
0
投票

由于用户是云firestore中的文档,因此您可以根据documentatin https://firebase.google.com/docs/firestore/query-data/get-data以这种方式检索单个文档

来自文件: -

以下示例显示如何使用get()检索单个文档的内容:

    var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
    } else {
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});

在您的情况下,您可以执行以下操作: -

     const db = firebase.firestore()

      db.collection('users')
     .doc('currentUserID') // change to the current user id 
     .get().then((user)=>{
         if(user.exists){
             // now you can do something with user
             console.log(user.data())
         }
     })

0
投票

要从集合中获取用户,您需要登录用户的UID。 Auth()对象尚未在“Firestore数据库触发器”中实现。 (它是为firebase RTD实现的)

在这里看到我的答案:

Getting the user id from a Firestore Trigger in Cloud Functions for Firebase?

© www.soinside.com 2019 - 2024. All rights reserved.