Error parsing the JSON data was encountered

I encountered an error while parsing json data. Here is the json snippet:

    [{
    "name": "TqgQdaaqQU",
    "location": "YEyFMkCiAjGRsxa",
    "type_id": 68,
    "gos_prinad_id": 64,
    "disloc_country_id": 95,
    "adm_terr_id": 57,
    "lat": 142.48033105609792,
    "lon": 90.31768510181121,
    "alt": 483,
    "hops": [{
        "hop_type": "KZMzTuUnNw",
        "id_sl_hop": 928,
        "hop_text": "GZOvhipRpTgwPDkeGKrl"
    }]
}]

Here is a portion of my template code related to the <div> block

    <tbody>
       <tr *ngFor='let list of lists'>
          <td>{{ list.name }}</td>
          <td>{{ list.location }}</td>
          <td>{{ list.type_id }}</td>
          <td>смотр.</td>
          <td>{{ list.lat }}</td>
          <td>{{ list.lon }}</td>
          <td>{{ list.alt }}</td>
       </tr>
       <tr>
          <td colspan="6"></td>
          <td colspan="2">Страниц: из </td>
          <td colspan="2">Просмотр: из </td>
       </tr>
       <tr>
          <td>1</td>
          <td>2</td>
          <td>3</td>
       </tr>
    </tbody>
   </table>
   <div>
      <br>
      <table>
         <thead>
              <tr>
                  <th>Тип ХОП</th>
                  <th>Текст ХОП</th>
                  <th>Номер ХОП</th>
               </tr>
          </thead>
          <tbody>
            <tr *ngFor="let new_var of list.hops">
                <td>{{ new_var.hop_type }}</td>
                <td>{{ new_var.id_sl_hop }}</td>
                <td>{{ new_var.hop_text }}</td>
             </tr>
           </tbody>
          </table>
   </div>

Although I have no issues with using {{list.name}} and others, I am facing an error specifically with {{new_var.hop_type}} displaying

TypeError: Cannot read property 'hops' of undefined
. Any assistance would be greatly appreciated!

Answer №1

The error occurs because the variable list is a local template variable that only exists within your first <tr> tag due to the use of the let keyword. To resolve this issue, you can modify your code as follows:

<table>
<tbody *ngFor='let list of lists'>
   <tr>
      <td>{{ list.name }}</td>
      <td>{{ list.location }}</td>
      <td>{{ list.type_id }}</td>
      <td>смотр.</td>
      <td>{{ list.lat }}</td>
      <td>{{ list.lon }}</td>
      <td>{{ list.alt }}</td>
   </tr>
   <tr>
      <td colspan="6"></td>
      <td colspan="2">Страниц: из </td>
      <td colspan="2">Просмотр: из </td>
   </tr>
   <tr *ngFor="let new_var of list.hops">
      <td>{{ new_var.hop_type }}</td>
      <td>{{ new_var.id_sl_hop }}</td>
      <td>{{ new_var.hop_text }}</td>
   </tr>
</tbody>
</table>

By making this change, you will be able to access the list variable in your second *ngFor iteration and properly display all of your data.

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

Encountering errors while working with React props in typing

In my application, I am utilizing ANT Design and React with 2 components in the mix: //PARENT const Test = () => { const [state, setState] = useState([]); function onChange( pagination: TablePaginationConfig, filters: Record<string, ...

Using Typescript and React to retrieve the type of a variable based on its defined type

Just getting started with Typescript and could use some guidance. Currently, I'm using React to develop a table component with the help of this library: Let's say there's a service that retrieves data: const { data, error, loading, refetc ...

type of key extractor is unknown in React Native

How can I specify a type for the renderItem function of a FlatList in React Native? This is my current approach: // Importing the generic type for the FlatList render item function import { ListRenderItem } from "react-native"; // Assigning the ...

The index.ngfactory.ts file threw an unexpected token error, indicating that an appropriate loader may be necessary to handle this specific file

I've spent several hours trying to troubleshoot this persistent error, exhausting all online resources for solutions. The issue arises consistently with every module of Angular Material only during the build process using --env.prod. The webpack confi ...

Strategies for successfully passing and showcasing the result of a calculation on a fresh view or component within Angular

I have developed a basic calculator application with two main components: Calculator and Result. The Angular router is used to navigate between these components. Now, I am looking for a way to dynamically display the calculation result from the Calculator ...

What is the best way to access the input element of ng-content from the parent component?

Creating a unique component in the following structure <div class="custom-search-field" [ngClass]="{'expanding': expanding}"> <ng-content></ng-content> </div> When using this component, users are expected to include ...

What is the method for obtaining the return type based on the type of a generic function?

Within my api function, I utilize a parser function that is generic and typically returns the same type as its input. However, in some cases, this may be different for simplification purposes. When using the api function, I am able to determine the type t ...

Creating a customizable filter by using the function of an object

I am currently developing a customizable dynamic filter system, similar to the functionalities offered by Yahoo Screener. To achieve this, I have defined an interface for the available filter fields: export interface FilterField { label: string; se ...

Learn how to display a tooltip for every individual point on a Highcharts network graph within an Angular

I am currently working on developing network graphs using highcharts and highcharts-angular within my Angular application. I have successfully managed to display the graph with datalabels, but now I need to implement tooltips for each point or node on the ...

In Angular 4, display a "show more" option within a table cell if there is an abundance of characters present

I have a code structure that looks like this: <mat-table> <ng-container matColumnDef="messageText"> <mat-header-cell *matHeaderCellDef> {{ 'CUSTOMER.MESSAGES_LIST_TABLE.MESSAGE' | translate }} </mat-header-ce ...

Tips for configuring the navigation links on the current page using Bootstrap

In my Angular application, I have set up navigation links for home, about, notifications, and logout. However, when I click on the home link, it redirects me to the login page instead of remaining on the current page. I need the functionality to stay on ...

How can I use TypeScript to wrap a component in Vue 3?

Looking to customize a PrimeVue component (Calendar) by styling it differently and then re-exporting it. Here's an example in React: const WrappedCalendar: React.FC<CalendarProps> = (props)=> <div style={{background:'green'}}&g ...

Exploring Angular 2 Application Testing: Tips for Interacting with HTML Elements

In my Angular 2 Frontend testing journey, I came across a blog post ( ) where the author utilized ng-test TestBed for core testing in Angular. While the example provided was helpful for basic understanding, it lacked details on how to manipulate Elements. ...

Embedding an Iframe in Angular 2 directly from the database

Looking for assistance with iframes in Angular 2. Initially, embedding an iframe directly into a component's template functions correctly. <iframe src='http://plnkr.co/edit/zZ0BgJHvQl5CfrZZ5kzg?p=preview | safeUrl' allowtransp ...

Specialized purpose for typed arrays

Given an array containing elements of two different entities: interface A { type: string } interface B { type: number } const a = {} as A const b = {} as B const array = [a, b] The array is of type (A | B)[] How can we create a utility type that ...

having trouble retrieving the values of the current item during a submit event

When working with a JSON array of objects that are bound to an HTML form and a mat-expansion-panel view, I encountered an issue where I couldn't retrieve the updated value of the name input field. No matter what I tried, it always returned the initial ...

Explore q.all for managing Angular 2 observables

Is there a method similar to q.all in Angular 2 to handle multiple HTTP API requests? In Angular 1, I would do the following: var promises = [api.getA(), api.getB()]; $q.all(promises).then(function(response){ // response[0] --> A // response[ ...

Incorrectly aligned highlighted labels are appearing on the Kendo chart category axis item labels

My current project involves using the kendo chart to generate charts, and I need specific labels to show up as bold text. To achieve this, I am utilizing the visual method which allows me to customize labels and render them with createVisual(). However, w ...

Having trouble with Angular 2 and localhost/null error while attempting to make an http.get request?

In my Angular 2 webpage, I am using the OnInit function to execute a method that looks like this (with generic names used): getAllObjects(): Promise<object[]>{ return this.http.get(this.getAllObjectsUrl).toPromise().then(response => response. ...

Tips for building a dynamic view using Angular

I am working on a project where I need to dynamically generate multiple horizontal lines using data from a JSON file, similar to the example in the image below. https://i.sstatic.net/MthcU.png Here is my attempt: https://i.sstatic.net/EEy4k.png Component. ...