简体   繁体   English

如何在 Google Firestore 中获取当前登录用户的文档

[英]How to get document by current signed in user in Google Firestore

I am trying to get a specific document that is created when a user signs up.我正在尝试获取用户注册时创建的特定文档。 I made it so that when the use signs up, their firebase user ID is the name of the document.我这样做是为了在用户注册时,他们的 firebase 用户 ID 是文档的名称。

I can't seem to figure out how to call that document.我似乎无法弄清楚如何调用该文件。

I have tried: firebase.auth().currentUser.uid;我试过:firebase.auth().currentUser.uid; uid userId (from computed) and several other related things to the uid. uid userId(来自计算)和其他几个与 uid 相关的东西。

When I look for a solution online I see the exact examples I have tried and I am wondering if there has been a change to how uid works maybe?当我在网上寻找解决方案时,我看到了我尝试过的确切示例,我想知道 uid 的工作方式是否发生了变化?

<script>
// eslint-disable-next-line
import firebase from "firebase";
// eslint-disable-next-line
import firestore from "firestore";
import db from "@/components/fbInit";

export default {
  components: {},
  data() {
    return {
      users: []
    };
  },
  computed: {
    userId() {
      return firebase.auth().currentUser.uid;
    }
  },
  created() {
    db.collection("userProfiles")
       .document(userId)
       .get()
       .then(querySnapshot => {
         querySnapshot.forEach(doc => {
          const data = {
            id: doc.id,
            name: doc.data().name,
            company: doc.data().company,
            state: doc.data().state
          };
          this.users.push(data);
         });
      });
  }
};
 </script>

Usually I get no error but it doesn't load any document.通常我不会收到任何错误,但它不会加载任何文档。 Sometimes it will say "uid" or whatever I am trying to use is undefined.有时它会说“uid”或者我试图使用的任何东西都是未定义的。

This is the solution for anyone else wondering.这是其他任何想知道的人的解决方案。 unique is a field I set to match the user ID when a user is created. unique 是我在创建用户时设置为匹配用户 ID 的字段。 Alternatively I believe you can use author to get the author of the document.或者我相信你可以使用 author 来获取文档的作者。

var user =  firebase.auth().currentUser


db.collection("userProfiles").where('unique', '==', user.uid).get()
  .then(querySnapshot => {
    querySnapshot.forEach(doc => { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM