User doc in Pinia with Nuxt #1285
-
I have a Here's what I have so far... stores/userStore.ts import { doc } from "firebase/firestore";
export const useUserStore = defineStore("UserStore", async () => {
const currentUser = await getCurrentUser();
if (currentUser) {
const userDocRef = doc(db, "users", currentUser.uid);
const user = useDocument(userDocRef);
return { user };
} else {
return {};
}
});
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useUserStore, import.meta.hot));
} index.vue <script setup lang="ts">
const userStore = useUserStore();
const userDoc = userStore.user;
</script>
<template>
<div>
<h1>Index page</h1>
<pre>{{ userDoc }}</pre>
</div>
</template> I'm also using a very basic default.vue file. The issue I'm running into is in index.vue I'm getting an error on the 3rd line:
I'm using nuxt-vuefire 0.1.5, vuefire 3.0.1, @pinia/nuxt 0.4.6, pinia 2.0.29, and nuxt 3.1.0. Any idea what's going on? Is there a better way to structure my code? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The problem is const currentUser = useCurrentUser()
const userDocRef = computed(() => doc(db, 'users', currentUser.value?.uid))
const user = useDocument(userDocRef)
return { user } |
Beta Was this translation helpful? Give feedback.
The problem is
const currentUser = await getCurrentUser();
the setup function of a store must be sync. Use the current user instead. You shouldn't have a conditional return in a setup function either, it's just not meant to be used that way. What is exposed should be stable.