Dealing with undefined properties in Javascript can cause errors

[ { dateTime: '2016-03-30 05:55:53', value: '4.0' },
  { dateTime: '2016-03-30 05:55:55', value: '17.0' },
  { dateTime: '2016-03-30 05:55:57', value: '17.0' },
  { dateTime: '2016-03-30 06:00:51', value: '33.0' } ]

the provided values are stored in the console.log(points.points[tags[0]]) variable, however attempting to access a specific item like console.log(points.points[tags[0]][0]) throws an error as follows: { dateTime: '2016-03-30 06:00:51', value: '33.0' }

when trying to print the individual data elements such as console.log(points.points[tags[0]][0].dateTime), an error occurs with the messages "Cannot read property 'dateTime' of undefined" and "Cannot read property 'value' of undefined".

var data = []; 
        <%
       for (var i=0; i < tags.length; i++)
       { %>
       var dataSeries = { type: "line" };
        var dataPoints = [];

            // var pointdata ="<%= points.points[tags[i]] %>";


        for (var n=0; n < <%= points.points[tags[i]].length %>; n++)
        {

          <%=   console.log(points.points[tags[i]]); %> 

           dataPoints.push({

                 x: <%= points.points[tags[i]][n].dateTime %>,
                 y: <%= points.points[tags[i]][n].value %>
            });
        }
             dataSeries.dataPoints = dataPoints;
             data.push(dataSeries);               
          <% } %>

    </script>

Answer №1

Experiment

points.points[tags[0]][0]["dateTime"]

This could be worth trying out as it utilizes a dictionary data structure.

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

Convert a Node.js module from synchronous to asynchronous functionality

I recently developed a Node.js module that is designed to handle Handlebars templates. It reads a directory of these templates, compiles them, and then exports an object containing the templates with their names as keys: 'use strict'; var fs ...

What is the process for setting up a new router?

When it comes to templates, the vuestic-admin template from https://github.com/epicmaxco/vuestic-admin stands out as the perfect fit for my needs. However, I have a specific requirement to customize it by adding a new page without displaying it in the side ...

Customizing the Class of a jQuery UI ui-autocomplete Combobox Container

Is there a way to customize the ui-autocomplete div by adding a custom class? I have several autocomplete widgets on my webpage, and I need to style their drop-downs differently. Since editing the ui-autocomplete class directly is not an option, I am wor ...

Show the total sum of a specific column in a datatable and enable the option to export the data to an Excel

When exporting a datatable as an Excel file with 4 columns, the third column contains product prices. After the export, I would like to see an additional row at the end of the table labeled "Total" that displays the sum of all values in column 3. I initia ...

Creating a JSON representation of a domain and its connections in Grails

I'm having trouble displaying a domain and its relationships as JSON in my Grails 3.3.8 project. So far, I have a domain called Round with multiple RoundItem instances, and I've also added another domain named AnswerOptions that is associated wi ...

Saving the state of checkboxes in Angular Material

I am trying to figure out a solution for storing checkbox values, specifically whether they have been checked before or not. For example, I have this dialog: After clicking on a checkbox and then clicking Save, the checkboxes reset to being unchecked when ...

When using Express, the XML response is returning an empty document

I'm experimenting with a simple API that returns XML response: const express = require('express'); const bodyParser = require('body-parser'); const cors = require('cors'); const libxmljs = require("libxmljs"); const PO ...

The Full Screen Button on Google Maps isn't functioning properly in a third-party mapping application

Below you will see the '+' icon which is also supposed to function as the full screen button. https://i.stack.imgur.com/IMvHa.png However, when clicked on, it does not expand to full screen as intended. I have attempted using some basic jQuery ...

Is a fresh connection established by the MongoDB Node driver for each query?

Review the following code: const mongodb = require('mongodb'); const express = require('express'); const app = express(); let db; const options = {}; mongodb.MongoClient.connect('mongodb://localhost:27017/test', options, fu ...

Creating a dynamic cascading dropdown list with Vue.js: Step-by-step guide

I successfully implemented a dropdown list using Vue.js, but now I want to add another similar list. How can I set this up? Here are the codes for both dropdown lists: var addUserVue = new Vue({ el: "#app", data: { heading: "Vue Select Cas ...

Understanding how to parse JSON in a Node.js environment is

I'm currently investigating why the parsed request, a JSON from a jQuery AJAX call made across domains, appears to be a bit unusual. GET /sendjsondata?callback=_testcb&{%22amount%22:1800,%22name%22:%22Vasy%20Jon%22,%22cnp%22:232323,%22coborrower% ...

Implementing the OnClick method for the button component

After successfully creating a reusable button component, I now want to assign different onClick links to each Button component. How can I achieve this? import styled from 'styled-components' const Button = styled.button` background: #0070f3; ...

Can Javascript (PWA) be used to detect fake GPS or mock GPS in applications?

Looking for a solution to prevent users from using Fake Location tools in my PWA application that gathers absence location data. Is there a method or package in JavaScript to detect the presence of Fake GPS installed on the device? ...

What is the best way to compare two arrays of ids in MongoDB to find matching elements?

I have 2 arrays with different ids : bikesWithNoOrders [id , id1 , id2] filteredResult [id3 , id5] Is there a way to query and find all of them at once? This is what I currently have: queryBuilder.find({ _id: { $in: bikesWithNoOrders } }); queryBuilde ...

What causes the variable to be undefined in the method but not in the constructor in Typescript?

I am currently working on an application using AngularJS 1.4.9 with Typescript. In one of my controllers, I have injected the testManagementService service. The issue I'm facing is that while the testManagementService variable is defined as an object ...

Unlawful use of Unicode characters

I am currently experiencing an issue while attempting to upload the document.sdf (json) file to Amazon Cloud Search. Everything works seamlessly until certain special characters are encountered. Encountered Unicode characters that are not compliant with C ...

What is the best way to empty input, select, and textarea fields after performing a clone operation

I am currently working on creating a form to display items and need a fresh text field when the user clicks the ADD button. function addNewItem() { //ADD ROW AND CLONE var table = document.getElementById('tbl'); var tbody = document.create ...

The custom error page in NextJS is failing to display

In my custom pages/404.ts file, I have coded the following: export default function NotFound() { return <h1>404 - Page Not Found</h1> } Additionally, there is another page that displays a 404 error when the organization is null: import Error ...

I'm currently attempting to establish a connection between my server.js express API and MongoDB, but I keep encountering an unfamiliar error message that I'm having trouble decipher

Here is the server.js code: import express from 'express'; import bodyParser from 'body-parser'; import userRoutes from './routes/users.js'; import mongoose from 'mongoose'; co ...

Tips on obtaining the JSON format of an object containing an array variable

In my class, I have three variables including an array. I created a method specifically for use with json_encode(). public function getJSONString(){ return [ 'id' => $this->id, 'name' => $this->name, ...