Utilizing Vue to Customize Chart Settings with Data

I'm currently working on hitting a rest api to retrieve a dataset for a specific piece of equipment. However, I'm struggling to extract the first column of data as an array. I've attempted using computed and methods in vue, but I constantly receive an empty array []. Can anyone shed some light on what might be going wrong? The {{datas}} tag prints out my JSON without any issues, but when I add {{getDate}}, it returns "can't find date of undefined."

Thank you

<template>
  <v-app>
    <p>{{ datas }}</p>
    {{ getDates }}
    <center>
      <chart :options="chartOptionsBar"></chart>
    </center>
    <chart :options="chartOptionsLine"></chart>
    <p>108J View</p>
  </v-app>
</template>

<script>
import axios from "axios";

export default {
  name: "E108J",
  components: {},
  data() {
    return {
      datas: [],
      chartDates: [],
      chartOptionsLine: {
        // Chart options
      },
      chartOptionsBar: {
       // Bar chart options
      }
    };
  },
  mounted() {
    axios
      .get("http://localhost:3000/E108J")
      .then(response => (this.datas = response.data));
  },
  computed: {
    getDates() {
      let chartdates;
      for (let i = 0;i< this.datas.length; i++) {
        chartdates = this.data[i];
      }
      return chartdates;
    }
  }
};
</script>

JSON Data:

[
{
// Sample JSON data
},
{
...
}
]

Answer №1

Ensure the safety of your code by first checking if the variable datas exists in your computed property. Although it is initially set as an array, receiving invalid data could potentially trigger errors. To prevent this, you can include a validation check within your computed property like

if(this.datas && this.datas.length)
. If that condition is met, proceed to populate chartdates; otherwise, return an empty array.

In the provided JSON example, remember to extract the date properly from the for loop using:

this.datas[i].date

The current implementation retrieves the entire object within { .. } instead of just the date. Additionally, there is a mistake where this.data[i] should actually be this.datas[i].

To convert chartdates into an array, assign each value to chartdates[i] accordingly. Failure to do so will only store the last value in chartdates, as mentioned by Steven.

Answer №2

Here is a suggested solution:

<div v-if="datas.length > 0">
<p>{{ datas }}</p>
 {{ getDates }}
</div>

Answer №3

Within the following code snippet

retrieveDates() {
  let chartdates;
  for (let i = 0;i< this.dataSet.length; i++) {
    chartdates = this.dataset[i];
  }
  return chartdates;
}

consider replacing this.dataset[i] with this.dataSet[i];

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

Highlighting table rows when hovering in IE with JQuery - compatibility across all versions

I have a table on my jsp page with multiple rows and columns. I want to ensure that visitors can easily follow along when they interact with the table - by highlighting the row or column they hover over with a different background color. Although the **hov ...

What causes the error message "ng-model is undefined"?

Within my form, there are three angular-ui-bootstrap typehead elements <form> <div class="form-group"> <label for="fieldname" class="col-md-3 control-label">Name</label> <div class="col-md-6"> <input type="text ...

Is it necessary to include a promise in the test when utilizing Chai as Promised?

Documentation provided by Chai as Promised indicates the following: Note: when using promise assertions, either return the promise or notify(done) must be used. Examples from the site demonstrate this concept: return doSomethingAsync().should.eventua ...

Purge the Nuxt store along with all its submodules upon logging out

I am pondering the most effective way to reset my entire store whenever a user logs out. In my app, there are situations where I switch accounts in dev mode, similar to what can happen in live mode. However, I encounter issues when logging in with a differ ...

Generating dynamic variable names in JavaScript

Looking for a similar solution. var item_<?php echo $variable->n ?> = <?php echo '32' ?> Trying to achieve something like this: var item_342 = '32' ...

The browser message states: Variable $? Not Found

As a newcomer to javascript (jquery/json), I've created code to display a chart using CanvasJS with a php/json data fetching script. However, I'm facing an issue where the chart is not rendering when I integrate my code. Upon inspecting the brows ...

Guide on how to create a custom response using class-validator in NestJS

Is it feasible to customize the error response generated by class-validator in NestJs? The default error message structure in NestJS looks like this: { "statusCode": 400, "error": "Bad Request", "message": [ { "target": {} ...

Ways to merge multiple cells horizontally in a table right from the beginning

Is there a way to start the colspan from the second column (Name)? See image below for reference: https://i.stack.imgur.com/RvX92.png <table width="100%"> <thead style="background-color: lightgray;"> <tr> <td style="width ...

Utilize Nuxt 3 dynamic components to trigger API calls using route parameters

I am in the process of developing a Nuxt 3 application for educational purposes that involves utilizing dynamic routes to fetch data from an API upon page load. My main focus is on leveraging the route parameter id with the composition API to retrieve exte ...

Numerous web worker asynchronous requests are being made, but not all are coming back

Within my web worker script, I am utilizing the following code: self.addEventListener('message', function(e){ try { var xhr=new XMLHttpRequest() for(var i = 0; i < e.data.urls.length;i++){ xhr.open('GET&apos ...

The appearance of HTML varies depending on the type of mobile device being used

My email template is having display variations on different mobile devices, with the textbox sometimes centered instead of left aligned as intended. Any suggestions on what might be causing this issue and how to resolve it? Desired Display on All Devices ...

Juggling PHP scripts within javascript

Can you help me with a question I have? I am working on multiple JS scripts, such as this one: <script> $('#mapveto1').click(function() { $('#mapveto1').addClass('banned'); $('#map1').text('cobble ...

How can I effectively send a form with the value of 'put' using Laravel and Ajax?

I have been working on a function that utilizes AJAX to validate and send responses to the view when an edit form is submitted. However, I am encountering an issue where the page loads on a new URL like localhost:8000/comment/id, and I want the page to dis ...

Express application failed to deploy due to boot timeout error R10

After researching extensively on Stack Overflow and various other websites, I have been unable to solve a problem with my small Express app using ES6 syntax. Upon deploying it to Heroku, I encountered the following error: LOGS : 2021-04-09T12:14:14.688546 ...

Solving the pyramid of doom with Node.js

I have been struggling to find a way to simplify my code structure. Here is what I have come up with so far: router.use('/create', function(res,req,next) { try { var data = { startDate: new Date(req.body. ...

Event handlers in JQuery are not connected through breadcrumb links

Wondering how to ensure that the click handler is always attached in my Rails 4.1 app where I am using JQuery-ujs to update cells in a table within the comments#index view. In my comments.js.coffee file, I have the following code snippet: jQuery -> ...

The Wordpress admin-ajax.php script is failing to process the function and returning a "0" error code

I have been experimenting with processing AJAX requests in Wordpress and I'm following a particular tutorial to achieve this. The goal is to create a basic AJAX request that will display the post ID on the page when a link is clicked. The Approach ...

The Next.js app's API router has the ability to parse the incoming request body for post requests, however, it does not have the

In the process of developing an API using the next.js app router, I encountered an issue. Specifically, I was successful in parsing the data with const res = await request.json() when the HTTP request type was set to post. However, I am facing difficulties ...

An error occurred while trying to load the resource in the Redux-Saga: connection refused

Utilizing axios allows me to make calls to the backend server, while redux-saga helps in managing side effects from the server seamlessly. import {call, put, takeEvery} from "redux-saga/effects"; import {REQUEST_FAILED, REQUEST_SUCCESS, ROOT_URL, SUBMIT_U ...

Is there a way to mimic disabled input fields?

Is there a way to simulate input being disabled without actually changing the value in the input field, but still have that value sent with POST using a form? <input class="dis" type="text" disabled="disabled" value="111"> <input class="no" type= ...