[英]$ Uncaught (in promise) TypeError: Cannot read properties of null (reading 'uid') at setup - Quasar 2, Firebase 9 Authentication, Vue.js 3, JavaScript
I don't have enough for putting a bounty, but I may send a $donation$ to whoever helps me answer this!我没有足够的赏金,但我可以向帮助我回答这个问题的人发送 $donation$!
GOAL: Render page with user's list on refresh.目标:在刷新时呈现带有用户列表的页面。
PROBLEM: On refresh, the page does not catch the user id from Firebase before the page renders.问题:刷新时,页面在页面呈现之前没有从 Firebase 捕获用户 ID。
ERROR: Cannot read properties of null (reading 'uid')错误:无法读取 null 的属性(读取“uid”)
How can I get the user ID from firebase before the page loads to the DOM?如何在页面加载到 DOM 之前从 firebase 获取用户 ID? It works fine on initial navigation to the List Page, however, when I press refresh it gives the above error.
它在初始导航到列表页面时工作正常,但是,当我按下刷新时,它会出现上述错误。 I need the 'uid' to refresh the list of items.
我需要“uid”来刷新项目列表。 This is a simplified version of my code and something I tried below.
这是我的代码的简化版本,我在下面尝试过。
List.vue列表.vue
...
////////// The following line causes error //////////
const itemsRef = collection(db, 'users/' + auth.currentUser.uid + '/items')
onSnapshot(itemsRef, snapshot => {
let results = []
snapshot.docs.forEach(doc => {
results.push({ ...doc.data(), id: doc.id })
})
items.value = results
})
...
I tried:我试过了:
List.vue列表.vue
...
onAuthStateChanged(auth, ( user ) => {
if (user) {
const user = auth.currentUser
const uid = user.uid
////////// The following line causes error //////////
const itemsRef = collection(db, 'users/' + uid + '/items')
onSnapshot(itemsRef, snapshot => {
let results = []
snapshot.docs.forEach(doc => {
results.push({ ...doc.data(), id: doc.id })
})
items.value = results
})
}
})
...
I still get the same error:我仍然得到同样的错误:
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'uid') at setup
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'uid') at setup
Full simplified List.vue完全简化的 List.vue
<template>
<q-page padding>
<p>LIST</p>
<q-list
v-if="user"
bordered
separator
v-for="item in items"
:key="item.id"
>
<q-item>
<q-item-section>
<div class="column">
<q-item-label
>{{ item.name }}
</q-item-label>
</div>
</q-item-section>
</q-item>
</q-list>
<q-list v-show="!uid"> Loading ...</q-list>
</q-page>
</template>
<script>
import { ref } from 'vue'
import { auth, db } from 'src/boot/firebase'
import { collection, onSnapshot } from 'firebase/firestore'
import { onAuthStateChanged } from "firebase/auth"
export default {
name: "List",
setup() {
const user = ref(auth.currentUser)
const uid = ref(user.value.uid)
const item = ref({})
const name = ref('')
const items = ref([])
const id = ref(item.id)
onAuthStateChanged(auth, ( user ) => {
if (user) {
user.value = auth.currentUser
console.log('user.value: ', user.value)
const uid = user.value.uid
console.log('uid:', uid)
console.log('uid.value:',uid.value)
// GET ITEMS LIST FROM FIRESTORE
console.log('BEFORE ITEMS REF')
const itemsRef = collection(db, 'users/' + uid + '/items')
console.log('itemsRef: ', itemsRef)
onSnapshot(itemsRef, snapshot => {
let results = []
snapshot.docs.forEach(doc => {
results.push({ ...doc.data(), id: doc.id })
})
// UPDATE VALUES
items.value = results
})
}
})
//
return {
item,
name,
items,
user,
uid,
id
}
}
}
</script>
Ok, so I ended up just changing the path for 'items'.好的,所以我最终只是更改了“项目”的路径。 Instead of having 'items' as a sub-collection of 'users' (which was dependent on having the 'uid' in the path), I made 'items' a root collection with the 'uid' as a field.
我没有将“items”作为“users”的子集合(这取决于路径中是否包含“uid”),而是将“items”作为一个以“uid”作为字段的根集合。 So, this:
所以这:
const itemsRef = collection(db, 'users/' + uid + '/items')
is now this:现在是这样的:
const itemsRef = collection(db, 'items')
OMHeck!哎呀! I found the answer thanks to Sy3d
感谢 Sy3d,我找到了答案
How to mount quasar app to dom after firebase connection is initialized? firebase连接初始化后如何将类星体应用挂载到dom?
src/boot/auth.js src/boot/auth.js
import { auth } from 'src/boot/firebase'
export default async ({ app, router, store }) => {
return new Promise(resolve => {
const unsubscribe = authService.onAuthStateChanged(() => {
resolve()
unsubscribe()
})
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.