What could be the reason for the cardDetails component, which is supposed to display information received via a Vuex action, not appearing

Recently, I've been diving into Vue and Vuex while working on a small app that showcases events. Users can click on event cards to view more details using the card's ID. I've moved all the code to the Vuex Store, but I'm encountering issues with rendering individual cards. The error seems to be related to accessing the ID, but when I console log props.id, I can see the correct ID displayed (e.g., 123 for the first card I clicked on).

Here's a snippet from the EventList Component: https://i.sstatic.net/lmziR.png When I click on a card, I receive the following console error: https://i.sstatic.net/I1Dhd.png Below is the code snippet for the EventDetails component:

<template>
  <div class="event-card">
    <h2>You are on {{ $route.params.props.id }}</h2>
    <span>@{{ event.time }} on {{ event.date }}</span>
    <h4>{{ event.title }}</h4>
    <p>{{ event.description }}</p>
  </div>
</template>

<script>
import store from "@/store";
import { computed } from "@vue/reactivity";
import { onBeforeMount, onMounted, reactive, ref, toRefs } from "vue";
import { useStore } from "vuex";

export default {
  name: "EventDetails",
  props: ["id", "modelValue"],
  setup(props) {
    const state = reactive({
      events: [],
      event: {},
    });

    const message = ref("AsapRocky");

    console.log(props.id)

    onMounted(() => {
      store.dispatch('fetchEvent', props.id)
      });

    const event = computed(() => {
      return store.state.event;
    });

    return {
      event,
      message,
      ...toRefs(state),
    };
  },
};
</script>

Here's a snippet from the store code:

import { createStore } from 'vuex'
import apiClient from '../services/EventService';

export default createStore({
  state: {
    user: 'TommyDemian',
    events: [],
    event: {}
  },
  mutations: {
    SET_EVENTS(state, events){
      state.events = events;
    },
    SET_EVENT(state, event) {
      state.event = event; 
    }
  },
  actions: {
    fetchEvents({ commit }){
      apiClient
        .getEvents()
        .then((response) => {
          commit("SET_EVENTS", response.data)
        })
        .catch((error) => {
          console.log(error);
        });
  },
  fetchEvent({ commit }, id){
    apiClient.getEvent(id)
    .then((response) => {
      commit("SET_EVENT", response.data)
    })
    .catch((error) => {
      console.log(error);
    });
  }
},
  getters: {
  },
  modules: {
  }
})

Answer №1

The error message suggests that the issue with the id reference is actually located in {{ $route.params.props.id }} within your template.

It seems like you meant to access the component's id prop, which is not stored in the route parameters:

<!-- <h2>You are on {{ $route.params.props.id }}</h2> --> ❌
<h2>You are on {{ id }}</h2> ✅

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Receiving a 404 error when attempting to use the 'import' statement within a script tag labeled as a module

I am currently working on a NodeJS project that utilizes Webpack for bundling and Express for serving. Whenever a user visits the root domain, they are served an index.html file containing a <script> block in the footer with a type of module. index. ...

Is it necessary to always pause before I click?

Currently, I am in the process of writing tests for my website using WebdriverIO with Mocha and Chai. However, I encountered an issue where my element is not rendered before attempting to interact with it. it('select application', function(done) ...

Tips for triggering the API call only when a change is detected

#In the provided code snippet, the useEffect hook is being used to fetch data from an API using the apiData() function. The data retrieved consists of objects with properties such as id, name, parent name, and isActive. The isActive property is linked to ...

why is the sum coming out as an undefined number?

My challenge involves creating a table that should display the total price, however, it keeps showing NaN. The code snippet below outlines how the total price is calculated: import React from 'react'; const Total = (props) => { const {ite ...

When implementing multer in an express application, I encountered an issue where req.files appeared empty

Currently, I am facing some issues while attempting to upload various file types to the server using multer in an express application. Whenever I make the request, the server responds with a TypeError: req.files is not iterable. Upon investigation, I notic ...

Update the primary folder for the assets in the Vue build

Vue CLI 3.3 is what I'm using to develop projects for my vertical website with Vue. However, every time I build the project, the assets in dist/index.html always load from the root path. For example: <script src=js/chunk-vendors.b0f460c7.js>< ...

Issues with managing ajax response handlers

Just dipping my toes into the world of ajax and attempting to create a reusable ajax request function. According to Firebug, the request is successfully fetching the correct data from my php file. However, for some reason, the response isn't getting i ...

Can Javascript be used to identify the number of td elements in a table and specify column widths within the table tags?

Is there a way to automatically add the "col width" tag based on the number of td tags within a Table tag? For example, if there are 2 td's, then it should add 2 "col width", and if there are 3 then, 3 "col width", and so on. <!DOCTYPE html> &l ...

Angular 2 is not recognizing the element 'router-outlet'

I am currently utilizing universal-cli... This is how my app.node.module.ts appears: /** * This file and `main.browser.ts` are quite similar, for now! * By separating these, you can create logic, imports, etc that are "Platform" specific. * If you wis ...

Guide on updating data within a file at a specific position using JavaScript

I am faced with a challenge involving a file containing the following data, Test.txt, <template class="get" type="amp-mustache"> <div class="divcenter"> /////Need to append data at this point///// </div> </template> ...

I'm encountering a barrage of errors while attempting to compile my React project. I'm completely stumped as to what they mean or where to even start in resolving them

Here is the error message: WARNING in ./node_modules/@ethersproject/abi/lib.esm/_version.js Module Warning (from ./node_modules/react-scripts/node_modules/source-map-loader/dist/cjs.js): Failed to parse source map from 'C:\Users\seana\N ...

What is the best way for me to determine the average number of likes on a post?

I have a Post model with various fields such as author, content, views, likedBy, tags, and comments. model Post { createdAt DateTime @default(now()) updatedAt DateTime @updatedAt id String @id @default(cuid()) author U ...

Leverage JSON files for pagination in NextJS

I am currently developing a science website where the post URLs are stored in a static JSON file. ScienceTopics.json- [ { "Subject": "Mathematics", "chapters": "mathematics", "contentList": [ ...

Form submission issue with dynamically added input fields within a modal

I'm facing a problem where dynamically added input fields in a modal are not being included when the form is submitted. The scenario involves a modal with a form, where input fields are added dynamically upon clicking an "Add" button. Each input fiel ...

Utilizing live function for event handling in jQuery

Hello there, I have encountered an issue while using jQuery 1.3, specifically with the live event where the element "context" is bound to the default document root. Can you suggest a way to address this problem? According to the jQuery documentation: ...

Is the button inactive until an action is taken?

In my coding project, I am working with two buttons. I am trying to figure out a way to disable the second button until the first button is clicked. Does anyone have any suggestions on how to achieve this using a combination of JavaScript and CSS? ...

Exploring the functionality of v-chips within a v-text-field

I am trying to implement a search text field using vuetify's v-text-field, and I want to display the user input in a chip (such as v-chip or v-combo-box). However, v-text-field does not seem to support chips based on the documentation. Is there a work ...

Navigating between Vue Router pages triggers multiple events within the mounted() lifecycle of VueJS

Currently, I am immersed in a project using Electron with a Vue CLI setup and the Vue CLI Plugin Electron Builder. The overall functionality is working perfectly fine except for a peculiar bug that has recently surfaced. The issue arises when navigating b ...

Different techniques for retrieving elements generated by ng-repeat from their containing parent

Let's keep it simple - imagine we have a directive called headSlides. This directive's template includes an image that is being repeated using ng-repeat: <img class="bg" ng-repeat="image in images" ng-src="{{image.src}}"> I need to access ...

The cookie is not displaying in the web browser

Why are my cookies not showing in the browser? I have tried multiple times, but even though the backend is sending the cookie, it is not being stored in the browser. I have also attempted to use different browsers like Chrome and Microsoft Bing. In Postma ...