简体   繁体   English

无法从 VueJs 中的 firestore 文档中检索 lastName 和 firstName

[英]Unable to retrieve lastName and firstName from firestore document in VueJs

在此处输入图像描述 I am trying to display last Name and first Name from a document stored upon signup with a field displayName.我正在尝试使用字段 displayName 显示注册时存储的文档中的姓氏和名字。 I thought that having a displayName as a common key would allow me to retrieve last name and first name of the user signing up by making a request to firestore using the displayName property stored in the currentUser object that is returned once I have logged in. However, I cannot access the lastName from the firebase documents and would really appreciate help.我认为将 displayName 作为公用键可以让我通过使用存储在 currentUser object 中的 displayName 属性向 firestore 发出请求来检索注册用户的姓氏和名字,该属性在我登录后返回。但是,我无法从 firebase 文档访问姓氏,非常感谢帮助。

<template>
  <p>Process Payroll</p>
  <h1>{{ user.displayName }} </h1>
  <h1>{{ docs }} </h1>
</template>

<script>
import getUser from '@/composables/getUser'
import { ref, onMounted, watch } from 'vue'
import { projectFirestore, projectAuth } from '@/firebase/config'
import { useRouter, useRoute } from 'vue-router'

export default {
    setup() {
    const { user } = getUser();

    watch(()=> user, () => returnUser())

    const lastName = ref("");
    const firstName = ref("");
    const docs = ref([]);

    const returnUser = async () => {
      const res = await projectFirestore
        .collection("users")
        .where("displayName", "==", "Abe")
        .get();
      if (!error.value) {
        // check your response here.
        console.log(res);
        const doc = res.filter((userObj) => {
          if ("Abe" === userObj.data().displayName) {
            return userObj.data().lastName;
          }
        });
        docs.value = doc;
      }
    };

    onMounted(returnUser)

    return { user, docs, returnUser};
  },
}
</script>

There's only a blank array being returned in place of the lastName.只返回一个空数组来代替姓氏。 I also don't see any requests made by firebase to retrieve the information needed.我也没有看到 firebase 发出的任何检索所需信息的请求。 I get a reference error:error is undefined in my console as well as cannot read property displayName of null despite using a watch to make sure that the user object is uploaded after.我收到参考错误:错误在我的控制台中未定义,并且无法读取 null 的属性显示名称,尽管使用手表确保用户 object 在之后上传。 Can someone please help with major code changes to help me get through my errors and display the lastname from firebase. Please help.有人可以帮助我进行重大代码更改,以帮助我解决错误并显示 firebase 中的姓氏。请帮忙。 I am new to Vuejs.我是 Vuejs 的新手。

在此处输入图像描述

import { ref } from 'vue'
import { projectAuth } from '../firebase/config'

// refs
const user = ref(projectAuth.currentUser)

// auth changes
projectAuth.onAuthStateChanged(_user => {
  console.log('User state change. Current user is:', _user)
  user.value = _user
});

const getUser = () => {
  return { user } 
}

export default getUser

在此处输入图像描述

在此处输入图像描述

If you want to log all firstName + lastName combinations then try this:如果您想记录所有 firstName + lastName 组合,请尝试以下操作:

const returnUser = async () => {
  const res = await projectFirestore
    .collection("users")
    .where("displayName", "==", "Abe") // Case sensitive
    .get();

  const lastNameList = res.docs.map(d => `Abe ${d.data().lastName}`)
  console.log(lastNameList)
  docs.value = lastNameList;
}

Try changing the getUser function like this so displayName is always defined.尝试像这样更改 getUser function,以便始终定义 displayName。

const getUser = () => {
  // set default value to null
  return user ? { user } : { displayName: "NoUser" } 
}

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

相关问题 无法从 Firestore 数据库中检索某些字段 - Unable to retrieve some fields from Firestore Database 无法从 Firestore 获取时间戳文档字段 - Unable to get Timestamp document field from Firestore 如何从 Firestore 检索文档并将其分配给变量? [安卓] [科特林] - How to retrieve a document from Firestore and assign it to a variable? [ANDROID] [KOTLIN] VueJS 和 Firestore:更新文档属性发生两次 - VueJS and Firestore: Updating a document property happens twice 从 Firestore 获取文档数据后无法设置 state - Unable to set state after getting document data from Firestore 从 Firestore 获取单个文档 - Fetch Single Document from Firestore 如何从 firestore 文档中检索密钥(使用 swift 4/ios) - How do I retrieve a key from a firestore document (using swift 4/ios) Firebase Firestore - 如何从特定文档中检索数组字段并将这些数组值用于 map 文档 - Firebase Firestore - how to retrieve an array field from a specific document and use those array values to map documents 从 Firestore 文档中检索数组并将其存储到 Node.Js,然后将其用作令牌来发送通知 - Retrieve an array from a Firestore document and store it to Node.Js then use it as tokens to send notifications 我无法从云 firestore 访问文档字段并在 flutter 中的 statefulwidget class 中显示它 - I am unable to access document field from cloud firestore and show it in a statefulwidget class in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM