Using storeToRefs inside of another store is not working #2633
-
I am using nuxt3 and have a store In export const useUserStore = defineStore('user', () => {
const user = ref(null)
async function getUser() {
user.value = await fetch(/* fetch the user */)
}
return { user, getUser }
}) Then in my wishlist store export const useWishlistStore('wishlist', () => {
const { user } = storeToRefs(useUserStore())
async function getWishlistItems() {
// user is undefined
const { data } = await fetch(`/wishlistItems?userId=${user.value.id}`)
// ...
}
}) I can do the exact same thing like this and not destructure and then export const useWishlistStore('wishlist', () => {
const userStore = useUserStore()
async function getWishlistItems() {
const { data } = await fetch(`/wishlistItems?userId=${userStore.user.id}`)
// ...
}
}) What is causing this to happen? In the header component I use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This can happen when stores are being cross used because the store object is not created for one of the stores. In practice, just use the store as a whole directly, it will work in all scenarios. |
Beta Was this translation helpful? Give feedback.
This can happen when stores are being cross used because the store object is not created for one of the stores. In practice, just use the store as a whole directly, it will work in all scenarios.