Vue alert: Issue encountered in data() - "TypeError: Unable to convert undefined or null to object"

Can anyone help me figure out how to remove this warning? I suspect it's because I'm trying to manipulate an undefined object. Any assistance would be greatly appreciated! Thank you! Here is the code snippet:

<v-card class="ma-3 pa-3" v-for="item in state.monitorAll[0]" v-bind:key="item.countdown">
  <v-row>
    <div class="ma-3">Minutes until next stop: <v-avatar class="ml-2 blue white--text">{{ item.departureTime.countdown }}</v-avatar></div>
    <v-avatar v-if="typeof item.vehicle == 'undefined'" class="ma-3 ml-5 green"><v-icon class="white--text">accessible</v-icon></v-avatar>
  </v-row>
</v-card>

<script>
import axios from 'axios'
import {onMounted, reactive} from '@vue/composition-api'

export default {
  name: 'App',
  setup() {
    let state = reactive({
      wlData: '',
      monitorAll: [],
    })
     async function showData() {
      let config = {
        headers: {
          'Accept': 'application/json',
          'Access-Control-Allow-Origin': 'http://localhost:8080',
          'Content-Type': 'text/plain'
        }
      }
      const wlData = await axios.get('http://localhost:8080/ogd_realtime/monitor?rbl=832', config);
      state.wlData = wlData.data.data['monitors'];
      state.monitorAll = state.wlData.map(monitor => monitor.lines[0].departures.departure);
    }

    let startLoad = onMounted(() => {
      console.log('component is mounted!');
      showData();
    })

    return {
      state,
      showData,
      startLoad,
    }
  }
};
</script>

Answer №1

After some investigating, I was able to identify the root of the issue. The culprit turned out to be the onMounted function. It seems that the onMounted function is triggered before any value is populated in the showData() method. Though I'm not entirely certain. Thus, I made the decision to remove;

let startLoad = onMounted(() => { console.log('component is mounted!'); showData(); })

Instead, I opted to directly call the showData() function right after its asynchronous creation. Although I am unsure why this solution works or if it's the most optimal approach, at least the warning message has disappeared!

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

Tips for avoiding the freezing of bootstrap-select scroll when a large number of options are present

I have integrated a bootstrap-select with a total of 1000 options. However, I am encountering an issue where when I attempt to scroll down the list of options, it only goes down approximately 60 options and then freezes in that position. Can anyone provi ...

The transition from Vuetify3's VSimpleTable to VTable is ineffective and unsuccessful

The v-simple-table component has been renamed to v-table Old code that was using v-simple-table may not work correctly after the renaming. It is recommended to use v-data-table with the same data, as it works correctly. https://i.sstatic.net/3GVdYMWl.png ...

When the Model popup is activated, it is essential for elements to have an adequate color contrast

Our team is utilizing the axe dev tool for accessibility testing. We encountered an issue where elements behind a model popup (Kendo React model with position: absolute) were displaying insufficient color contrast errors. Upon changing the absolute positio ...

Ajax is invoked only one time

The system is set up to display a follow button and an unfollow button. Clicking the follow button should make the unfollow button appear, and vice versa. Currently, when you click "follow", the database updates and the follow button disappears while the ...

Having trouble storing the message ID within the user Object using Mongoose

My goal is to add a message to the messages property (an array) of a user object using Mongoose. However, I encounter an error when trying to save the user (user.save()). To troubleshoot, I added three console.log statements in the code below. Can anyone h ...

What steps can I take to troubleshoot why my data isn't appearing in Vue.js?

I'm currently diving into the world of vue.js and attempting to follow this tutorial where data is being successfully displayed. App.vue <template> <div class="container"> <Header title="Task Tracker" /& ...

The click function for the responsive navbar hamburger is not functioning properly

Having some trouble with the code not working in responsive mode. I've tested it on a 600px screen and the hamburger button doesn't seem to work (I click it and nothing happens). I've gone through both the CSS and JS multiple times but can&a ...

Even in the face of errors, Selenium with Node.js continues to run seamlessly. The challenge arises specifically with the 107.xx version of the Chrome browser and Chrome driver

Currently, I am employed in a project involving NODE JS (javascript) with selenium webdriver. Package.json- “chai”: “^4.3.6”, “chromedriver”: “^107.0.3”, “geckodriver”: “^3.2.0”, “mocha”: “^10.0.0”, “mochawesome”: “^7. ...

An in-depth guide to effectively unit testing a Node.js Express application

Looking to kickstart unit testing in my Node Express project. What's the most straightforward and convenient approach for this? ...

Disable the scroll bar on a bootstrap modal

<span class="education"style="font-size:170%;line-height:150%;">&nbsp;Education <br> <small style=" color:gray;font-size:60%;">&nbsp; Blue Ridge University,2012-2014 </small> <br> <sma ...

AngularJS and Handlebars (npm)

Is it possible for angularJS to function as a substitute for the "view engine" in nodeJS? I am seeking insights on the optimal method to use. (Do MEAN Stack developers utilize view engines? Or do they prefer using res.sendFile along with tools like ui-ro ...

Help kids find solutions

Here is the HTML code I am working with: <div class = "touch" onclick="do(this)"> <span>text01</span> <span>text02</span> <span>text03</span> <span>text04</span> <div class = "findMe">a ...

Vue.js - Identify dynamic input fields as they are generated and modify their color when the value changes

I've set up a dynamic table with editable fields using Vue.js. Here's how it looks: <b-table :items="filtered" :fields="columns"> <template v-for="field in editableFields" v-slot:[tableCell(field.key)]="{ item }"> &l ...

React-Troubleshooting list items and keys: A comprehensive guide to resolving common issues

I'm facing a challenge with generating unique key ID's for my list items. Even though I thought I had a good understanding of how unique keys function, it seems that I am mistaken. In the code snippet below, using key={index} or key={item} is no ...

Display the div only when the time variable reaches zero

I want to display a div when the difference between the time imported from the database and the current time is 0. How can I achieve this? Here is the code snippet: while ($row = mysqli_fetch_array($result)) { echo "<div class='alert' id= ...

Searching for a point within a specified range using Sequelize

I have a model called Location with a field named coordinates of type geometry. I'm looking to create a function that takes in latitude, longitude, and radius as parameters, and returns all locations within that radius (in meters). I attempted to foll ...

How can you organize an array into rows and columns for easier viewing?

Hopefully everything is clear. I am open to making changes if needed to address any important points. In one of my components, the array items are displayed within cards as shown below: <b-card-group deck class="mb-3"> <b-card border-variant ...

How can I adjust the width of a handle/thumb for NoUiSlider?

Looking to adjust the width of a NoUiSlider using CSS: .noUi-horizontal .noUi-handle { width:8px; height:25px; left: 0px; top: -8px; border: 0px solid #000000; border-radius: 0px; background: #000; cursor: default; box- ...

Leveraging the Request npm package synchronously within Meteor 1.3

In my Meteor 1.3.2.4 project, I tried utilizing the synchronous features of the request npm package. I initially followed the instructions provided in this guide article from Meteor and attempted to use Meteor.bindEnvironment. Here's the code: impor ...

Access the $event object from an Angular template selector

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <input type="file" #myFile multiple /> <button (click)="onDelete(myFile.event)">DeleteFiles</button> My code snippet is experienci ...