[英]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.