Sorry, I'm unable to determine the value of 'title' because it is undefined

Here is the code for the "singleCard" component. When passing {{card.title}} and {{card.body}}, an error occurs:

**Error in render: "TypeError: Cannot read property 'title' of undefined"
found in
---> <SingleCard> at src/components/singleCard.vue
       <App> at src/App.vue
          <Root>**
<template>
    <div id="single-card">
        <h1>{{ card.title }}</h1>
        <article>{{ card.body }}</article>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                id: this.$route.params.id,
                card: {}
            }
        },
        created() {
            axios.get('http://jsonplaceholder.typicode.com/posts/' + this.id).then(response => {
                console.log(response);
                this.card = response.body;
            })
        }
    }
</script>

Answer №1

It appears that the data has not been fully loaded yet. Please attempt the following troubleshooting method:

<div v-if="card" id="single-card">
    <h1>{{ card.title }}</h1>
    <article>{{ card.body }}</article>
</div>

Answer №2

Modify response.body to response.data

<script>
    export default {
        data() {
            return {
                id: this.$route.params.id,
                card: {}
            }
        },
        created() {
            axios.get('http://jsonplaceholder.typicode.com/posts/' + this.id).then(response => {
                console.log(response);
                this.card = response.data;
            })
        }
    }
</script>

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

Opening a file upon launching in Nativescript-Vue: A step-by-step guide

After following the steps outlined in this article, my app is successfully able to open a file with a simple click. Upon launching the application, I see the following message in my console.log: (Foundation) [com.apple.foundation.filecoordination:claims] ...

showing text from chosen selection with an additional element included in the output

I could use some assistance with this code snippet. My goal is to showcase the options in the format: "Fried Rice = 10.000" as the outcome. However, the issue I am facing is that the select option box also contains the price. What I actually need is for th ...

Execute operations on element itself rather than the output of document.getElementbyId

I encountered an issue involving an HTML element with the ID of BaseGridView. When I call a function directly on this element, everything works perfectly. However, if I use document.getElementById() to retrieve the element and then call the function, it do ...

Ways to adjust the size of v-btn

Objective: My current task involves enhancing the customization options for Vue components, specifically focusing on adjusting the size of a button component based on user input. Our project utilizes Vuetify. Previous Code: <v-btn @click.prevent=&quo ...

Trigger the C# Click event with JavaScript fire function

Hi there, I could use a bit of assistance. My goal is to allow users to log in by hitting the "Enter" key on their keyboard. While I've successfully detected when the "Enter" key is pressed in my JavaScript code, I'm struggling with how to call m ...

What could be causing the "Failed prop type" error when dealing with nested cloned children, despite the parent having the correct initial values

Here is a question with two parts: What causes prop types checks to fail in a react-only scenario? How does a material-ui HoC interfere with type checking? When creating UI components, I keep the children unaware of each other by passing props through R ...

Finding and removing the last portion of the innerHTML can be achieved by employing specific techniques

Looking to manipulate a <div> element that includes both HTML elements and text? You're in luck. I need to locate and remove either the last, nth-from-last, or nth plain text section within it. For example: <div id="foo"> < ...

What is the best way to create operating system-neutral file paths in Node.js?

I'm currently fixing issues with my code while running integration tests on an EC2 instance of a Windows machine. Despite resolving the filenames-are-too-long problem, several paths remain unresolved due to being hardcoded for UNIX systems. I've ...

What is the best way to make a Modal triggered by a loop embedded within a table function properly on mobile devices?

While the modal is functioning properly on desktop, it seems to be encountering issues on mobile devices. The modal appears behind the background and is confined by the boundaries of the table (tbody). Below is the code snippet for triggering the modal: ...

The changing of colors does not function properly when clicked in JavaScript

I am having an issue with a drop-down list that offers two options: blue and green. When I select blue and click on the text input field, its background color alternates between blue and black (the default text field color). The same applies when I choose ...

Creating a custom date selection component in Angular 2 RC1

Can anyone recommend a datepicker that is compatible with Angular 2 RC1? I noticed that ng2-datepicker seems to be using angular2 RC1, but when trying to install it, it's asking for Angular 2 Beta. Would appreciate any assistance. Thank you in advan ...

Are there any resources available to ensure my jQuery script is compatible with multiple browsers?

After realizing that Internet Explorer does not support certain selectors in jQuery, I began to question how to ensure my code will function properly during the writing process. As a Linux user, my testing options are limited to Chrome and Firefox. Is ther ...

What is the best way to hide a <tr> element when its child <td> elements are empty?

I am facing an issue with a dynamically generated table that may have 'n' rows. If all <td> elements within a <tr> are empty, I want to hide or delete the entire row. My challenge lies in parsing through each <td> element to ch ...

Removing values in javascript for checkboxes that are not selected

Here is the JavaScript Code : var clinicalStat; var id; var val; var clinicalVals; $(":checkbox").click(function() { //alert(" you checked"); if ($(this).is(':checked')) { var checked1 = $(this).val(); //Inital value of check ...

JavaScript Variables Lose Their Values

Similar Inquiry: How can I get the response from an AJAX call in a function? I have written a function that fetches numerical data from an online file. Although the file retrieval is successful (verified by the alert message), I encounter an issue whe ...

The content within the tab is currently being loaded

Incorporating jQuery UI tabs into my webpage, I encounter a delay of 5-6 seconds when clicking on a tab as it triggers an ajax call for preparing and displaying content. The screen stays idle during this time, leading to user confusion. I am seeking a sol ...

Next.js throwing an error: TypeError - attempting to read property 'map' of undefined

I am facing an issue with my Next Js project. I have implemented the backend logic to display data, but when I attempt to show all the data using the useEffect hook and the map function, I encounter the following error: TypeError: Cannot read property &apo ...

Is there a way to store image URLs in a fashionable manner?

Hey there! I've been working on creating a HTML page that showcases multiple images. Everything is running smoothly on my localhost, but when I try to access it online, the images take forever to load. Any suggestions on how I can cache image URLs in ...

What is the best way to implement an array in Vuetify?

The content in the array is not displaying in the correct columns as expected from this code: <div id="app"> <v-app> <v-simple-table> <template v-slot:default> <thead> <tr> < ...

Incorporate a line break into a document using JavaScript

Is there a way to make all the items inside document.get on new lines without using paragraph elements? I have tried br/ and \n, but they do not work as expected. They either cause issues with JavaScript execution or fail to create new lines. <scr ...