Error: Trying to access the 'title' property of an undefined variable in Vue.js

Creating a replica of hackernews by utilizing the axios API. The NewItem.vue component is not receiving any data, resulting in an error — TypeError: Cannot read property 'title' of undefined. Can you identify what's causing this issue in the code? Why is the data not being passed through? Is it possible that the problem lies within the props?

NewItem.vue:

<template>
    <div class="news-item">
        {{ story.title }}
    </div>
</template>

<script>
export default {
  props: ['story'],
};
</script>

Home.vue:

<template>
  <div class="home">
    <div class="news-view">
      <div class="news-list-nav">
        <span>1/23</span>
      </div>
      <div class="news-list">
        <ul>
          <li class="news-item">
            <news-item v-for="story in stories" :key="story.id">
            </news-item>
          </li>
        </ul>
      </div>

    </div>

  </div>
</template>

<script>
import axios from 'axios';
import NewsItem from '../components/NewsItem.vue';

const BASE_URL = 'https://api.hnpwa.com/v0/news/1.json';
export default {
  components: {
    NewsItem,
  },
  data() {
    return {
      stories: [],
    };
  },
  created() {
    axios
      .get(BASE_URL)
      .then((response) => {
        this.stories = response.data;
      });
  },
};
</script>

Answer №1

The parent component needs to pass the prop to the child component in order for it to be accessible, like so:

<news-item v-for="story in stories" :key="story.id" :story="story"></news-item>

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

Ajax functionality is functioning properly in Firefox, however, it is facing issues in

Chrome seems to be having trouble while Firefox is functioning perfectly fine. The data is returning undefined in Chrome even when a simple string is being returned. This issue is happening within Wordpress. $('.vote-synergy-up, .vote-synergy-down&ap ...

Tips for showcasing varied information on Morris Chart

I am working with a Morris chart that is populated with a collection of data, each item containing 6 different data values. My goal is to switch between displaying two different sets of data. For example, I want to show the 'Target' data always ...

Steps for converting a window to a PDF file rather than an XPS file

Whenever I attempt to print the contents of my HTML page using window.print(), it always creates an XPS file. However, what I really need is for it to generate a PDF file instead. Any assistance would be greatly appreciated. Thank you! ...

Turn off the ability to click on images, but allow users to access the right

https://i.stack.imgur.com/jriaP.png Example image sourced from reddit.com Illustration represents the desired effect using CSS and possibly JS. In essence: I aim to make an image on a website unclickable to its imageURL There should be a context menu ...

Self-moving button container

I have a challenge involving a button and a sliding div. When the button is clicked, I want the div to slide. The problem arises when I place the button inside the sliding div - it ceases to slide. I understand the issue at hand, but I'm unsure of ho ...

Parse a JSON file in Python to make changes to a specific key's value, then proceed to save the updated data and send it

After extracting a JSON object with the following structure: { "board_title": "test", "read_only": false, "isIntegration": false, "board_bgtype": "board_graph", "created": "2017-08-16T06:40:47.158868+00:00", "original_title": "Revised_CID_Temp ...

The element in Vue 3 Unit Test is proving difficult to access

I am currently working on a Vue 3 project that utilizes PrimeVue. Within my template, I have integrated a PrimeVue dialog component as shown below: <template> <div> <Button data-testid="showButton" label="Show" @cli ...

Storing chosen choices from various select lists with similar class name in JavaScript

I am struggling to save all the selected options in an array for future output. Despite trying various suggestions, I haven't been able to make it work yet. The class names and names cannot be modified. HTML <select class="addon addon-select" nam ...

Mastering the use of swift protocols within model classes

JSON: "sittingFurniture":[ { "sittingObjectType": "chair", "fabric": "textile" }, { "sittingObjectType":"bed", "height": 70 }, ... ] Swift code: protocol Sitti ...

Converting a file full of JSON hashes into valid JSON using Ruby

Recently, I encountered a challenge where I received a text file from the server generated by an unalterable script. The contents of this file are formatted like "strings" of JSON data and my goal is to convert them into actual JSON using a Ruby script... ...

Decode JSON information into a designated structure

I have a JSON data that I want to unmarshal in Go: b := []byte(`{"Asks": [[21, 1], [22, 1]] ,"Bids": [[20, 1], [19, 1]]}`) My current approach involves defining a struct like this: type Message struct { Asks [][]float64 `json:"Bids"` Bids [][]fl ...

What is the process of using $ref and x-template references to connect a shared component to child components?

Currently, I am working on a project where users can post questions and provide answers. A post includes features like upvote, downvote, and comments. For instance, I need to figure out how to design this system. Imagine there is a comment button next to e ...

Generating variables dynamically within a React Native component

In my React Native component, I need to create a variable that will be used multiple times. Each instance of this component should have a different variable name for reference. <View ref={view => { shapeView = view; }} onLayout={({ nativeE ...

Executing a Select Change in a React Application using CasperJS

Has anyone else encountered difficulties with this issue? I have a basic React page set up, with a simple component that renders a select element and triggers a callback function when the value changes. Here is the basic structure of the component: const ...

leaflet.js - Place a marker when clicked and update its position while dragging

Currently working on a small project that requires the ability to place a marker on an image-map powered by leaflet.js, and update its position if it's dragged. I was using the code below for this functionality but keep getting an error 'marker n ...

Creating a Vertical Navbar Dropdown in Bootstrap 3.0 Without Appending to the Last List Item Only

Currently, I'm in the process of creating a panel layout that features an elegant vertical navbar. Although everything seems to be aligned correctly and I've managed to implement a dropdown menu in a vertical layout, it keeps appending to the las ...

Using getJSON to return key/value pair from local host URL in JSFiddle - A step-by-step guide

After following a tutorial on building an API using Python, Flask, SQLite, and SQLAlchemy, I have successfully tested the connection by hitting the localhost address in my browser. Now, I am curious if it is possible to test this connection and see the des ...

delivering axios response to display the page

I have a code snippet that requests data from an external API using axios and I want to incorporate the response into my rendered page. Here is my code: //Snippet from my controller required in main routes exports.recordBySlug = async (req, res, next) =&g ...

Leveraging Environment Variables in Vue.js

I've been diving into the official documentation, but haven't come across any information regarding environment variables. While there are community projects that offer support for this feature, they seem a bit excessive for my needs. I'm cu ...

JavaScript classList.remove function experiencing inconsistencies

Take a look at this fiddle: JSFiddle The HTML code: <table class="myTable"> <tr> <th>Some text</th> <td class="align"> <span class=" someStyle">1</span>/<span class="otherStyle">15</span& ...