When i using my project on local machine via “npm run dev”, everything is going well. But as soon as i upload the project to the server, an error appears.
I have a button, which start render basket-supplies, an errors occurs when i click him.
On local machine everything is fine(log in browser and IDE). So at server(vercel) i got mistake in dev-console when i click on button:
TypeError: Cannot read properties of undefined (reading 'cartStore')
at y (KV5dX6I7.js:1:9500)
at onClick (KV5dX6I7.js:1:15949)
at Zn (NWK0i2J7.js:14:38)
at nt (NWK0i2J7.js:14:108)
at HTMLButtonElement.n (NWK0i2J7.js:19:7629)
button:
<UiButton :variant="cartStore.isInCart(card.id) ? 'success' : 'action'"
@click="handleAddToCart(card.id, card.selectedRate)" class="mt-auto">
{{ cartStore.isInCart(card.id) ? 'В корзине' : 'В корзину' }}
<svg-icon v-if="cartStore.isInCart(card.id)" name="check-success" width="16" height="16" />
</UiButton>
function:
function handleAddToCart(cardId, selectedRate) {
if (!selectedRate) {
alert('Пожалуйста, выберите тарифный план.');
return;
}
this.cartStore.addItem(cardId, selectedRate.id);
}
environment properties:
import { useCartStore } from '@/stores/cart';
import { onMounted } from 'vue';
import cardsData from '~/src/data/cards-data.json';
import ratesData from '~/src/data/rates-data.json';
onMounted(async () => {
await Promise.all([
cartStore.setCardsData(cardsData),
cartStore.setRatesData(ratesData)
]);
});
const selectedRate = ref('');
Also pinia file cart.js:
import { defineStore } from 'pinia'
export const useCartStore = defineStore('cart', {
state: () => ({
cartItems: {}, // Объект вида { id: { count, rateId } }
cardsData: [],
ratesData: [],
}),
actions: {
addItem(id, rateId) {
if (this.cartItems[id]) {
this.cartItems[id].count++
} else {
this.cartItems[id] = { count: 1, rateId }
}
},
},
The code is presented partially, if necessary I will send the whole or the necessary part.
logged data transfer:
function handleAddToCart(cardId, selectedRate) {
// console.log('cartStore:', cartStore);
console.log('cardId:', cardId);
console.log('selectedRate:', selectedRate);
if (!selectedRate) {
alert('Пожалуйста, выберите тарифный план.');
return;
}
this.cartStore.addItem(cardId, selectedRate.id);
}
console.log('cartStore is', cartStore ? 'available' : 'undefined');
failed to catch logs on server from function:
addItem(id, rateId) {
console.log('Adding item:', { id, rateId })
if (this.cartItems[id]) {
this.cartItems[id].count++
} else {
this.cartItems[id] = { count: 1, rateId }
}
},
in other cases all logs are identical
Also, i’m tryied reinstall pinia from legacy package and add in package.json vue:latest config.
Roman Nemerсev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1