[英]typeerror cannot read properties of null reading uid, I have no asynchronous code and the users logged in any ideas?
I'm trying to create a user profile edit page where once you have logged in you are routed to this page and then the index returns your unique profile and not everyones profiles but I keep getting this error Uncaught (in promise) TypeError: Cannot read properties of null (reading 'uid').我正在尝试创建一个用户配置文件编辑页面,一旦您登录,您将被路由到此页面,然后索引返回您的唯一配置文件而不是每个人的配置文件,但我不断收到此错误 Uncaught (in promise) TypeError: Cannot read null 的属性(读取“uid”)。 I've looked up some of the reasons it could be and the user is logged in and i'm not using asynchronous code some I have no idea what the problem is I'm not sure if the uid isn't getting through before the function runs or what it could be?
我已经查找了一些可能的原因并且用户已登录并且我没有使用异步代码有些我不知道问题是什么我不确定 uid 是否在之前没有通过函数运行或者它可能是什么? Here is my vue.js script code on the page where it should display the profiles.
这是页面上应该显示个人资料的我的 vue.js 脚本代码。
<script>
import getCollection from '../Composables/getCollection';
import getUser from '../Composables/getUser';
import getPremium from "../Composables/getPremium.js";
const {Premium, error, load} = getPremium();
load();
export default{
setup() {
const { user } = getUser()
const { documents: Profile } = getCollection(
'Premium',
['userId', '==', user.value.uid]
)
console.log(Profile)
return { Profile }
}
}
</script>
<template>
<br><br>
<div v-if="error">{{ error }}</div>
<div v-if="Profile" class="Profile">
<p class="text-5xl text-red-700 font-serif">Your Profile Statistics</p>
<div v-for =" Premium in Premium" :key="Premium.id">
<p class="text-5xl text-red-700 font-serif">Your Profile Statistics</p>
<p class="text-5xl text-red-700 font-serif">{{ Premium.name }}</p>
<br><br>
</template>
Here is where I am getUser.js page.这是我所在的 getUser.js 页面。
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
And this is my getCollection.js page.这是我的 getCollection.js 页面。
import { ref, watchEffect } from 'vue'
import { projectFirestore } from '../firebase/config'
const getCollection = (collection, query) => {
const documents = ref(null)
const error = ref(null)
// register the firestore collection reference
let collectionRef = projectFirestore.collection(collection)
.orderBy('createdAt')
if (query) {
collectionRef = collectionRef.where(...query)
}
const unsub = collectionRef.onSnapshot(snap => {
let results = []
snap.docs.forEach(doc => {
// must wait for the server to create the timestamp & send it back
doc.data().createdAt && results.push({...doc.data(), id: doc.id})
});
// update values
documents.value = results
error.value = null
}, err => {
console.log(err.message)
documents.value = null
error.value = 'could not fetch the data'
})
watchEffect((onInvalidate) => {
onInvalidate(() => unsub());
});
return { error, documents }
}
export default getCollection
As I said neither contain an asynchronous function, the user is logged in it shows up in the console and I've been trying to figure out what the problem could be i've looked at examples but can't see any difference between them and mine, I've tried having it as a computed function too but that doesn't work either any help would be greatly appreciated, thanks.正如我所说,两者都不包含异步函数,用户已登录并显示在控制台中,我一直在试图找出问题所在我已经看过示例但看不到它们之间的任何区别和我的,我也试过将它作为一个计算函数,但它不起作用,任何帮助将不胜感激,谢谢。
Looking at your it is probably the getUser
function, which is asynchronous.查看您的可能是
getUser
函数,它是异步的。 Although is it not clear where this function is coming from in the code you provided.尽管不清楚您提供的代码中此功能的来源。 But it looks like that is returning a promise, you should await for.
但看起来这是在返回一个承诺,你应该等待。 You can use promise chaining for that, like:
您可以为此使用承诺链,例如:
setup() {
let Profile = null;
getUser().then(data => {
const user = data.user;
const { documents } = getCollection(
'Premium',
['userId', '==', user.value.uid]
)
Profile = documents;
}).catch(err => {
console.log(err);
})
console.log(Profile)
return { Profile }
}
}
getCollection
also sounds like a function that does a call to the server and probably is async. getCollection
听起来也像是一个调用服务器的函数,可能是异步的。
I had to have profile set as premium like this我必须像这样将配置文件设置为高级
<script>
import getCollection from '../Composables/getCollection';
import getUser from '../Composables/getUser';
import getPremium from "../Composables/getPremium.js";
const {Premium, error, load} = getPremium();
load();
export default{
setup() {
const { user } = getUser()
const { documents: Premium } = getCollection(
'Premium',
['userId', '==', user.value.uid]
)
console.log(Premium)
return { Premium }
}
}
</script>
<template>
<br><br>
<div v-if="error">{{ error }}</div>
<div v-if="Premium" class="Profile">
<p class="text-5xl text-red-700 font-serif">Your Profile Statistics</p>
<div v-for =" Premium in Premium" :key="Premium.id">
<p class="text-5xl text-red-700 font-serif">Your Profile Statistics</p>
<p class="text-5xl text-red-700 font-serif">{{ Premium.name }}</p>
<br><br>
</template>
Once I did this it was fixed.一旦我这样做了,它就被修复了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.