When attempting to call an API using the Angular HttpClient, an issue is encountered

I am having trouble displaying my JSON data in my application. I have imported the HttpClientModule in app.module.ts, but I keep getting an error that says:

"" ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.""

boat.json

{
    "Voilier": [
        {
            "img": "/assets/img/boat-img/voilier.jpg",
            "longeur": 10,
            "largeur": 20,
            "tirantEau": 50,
            "equipage": true,
            "annexe": false
        }
    ],
}

boat.service

 configUrl: string = 'assets/json/boat.json';

  constructor(private htpp: HttpClient) { }

  getBoat(): Observable<IBoat[]> {
    return this.htpp.get<IBoat[]>(this.configUrl);
  }

component.ts

public user: IBoat[] = [];

  constructor(private boatService: BoatService) { }

  ngOnInit(): void {
    this.boatService.getBoat()
      .subscribe(data => {
        this.user = data
      });
  }

html

 <tbody>
        <tr *ngFor="let bott of user">
            <td>{{bott.longeur}}</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </tbody>

Answer №1

My suggestion is to update the subscription code by assigning data.Voilier to the user property as shown below:

this.boatService.getBoat()
 .subscribe(data => {
   this.user = data.Voilier
 });

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

Problem encountered when using the Mongoose find() method with the exec() function

Could you please clarify why the code snippet below is returning audiences instead of an empty array? return Audience.find() .exec((err, audiences) => { if (err) return errorHandler.handle('audienceService', err); return Promise.re ...

The type of Object.values() is not determined by a union or Records

When utilizing TypeScript, the Object.values() function encounters issues in deducing the accurate type from a union of Records: type A = Record<string, number>; type B = Record<string, boolean>; function func(value: A | B) { const propert ...

JavaScript Elegance: Error Encountered - Unable to Locate Property 'index of' of an Undefined Value

As I delve into learning JavaScript through Eloquent Javascript, I encountered an error in one of the chapters that has me stumped. The error message "Cannot read property 'indexOf' of undefined" is appearing with this line of code: re ...

Is it possible to continuously update a Google line chart by programmatically adding rows at specific intervals using an AJAX call

Is there a way to dynamically add rows to the Google line chart each time data is retrieved via an AJAX call at set intervals? This is the existing code: google.charts.load('current', {'packages':['line']}); google.charts. ...

In a perplexing twist, requests made to the Express app arrive with empty bodies despite data being sent, but this anomaly occurs

Welcome to the community of inquisitive individuals on Stack! I'm facing an interesting challenge while developing an Express app. Despite everything running smoothly with two routes, I've hit a roadblock with one route that seems to have empty i ...

Encountering the "Not all code paths return a value" TypeScript error when attempting to manipulate a response before returning it, however, returning the response directly works without any issues

Encountering an issue where manipulating/process response and return triggers an error in TypeScript with the message "Not all code paths return a value.". Data is fetched from a backend API using RxJS lastValueFrom operator, along with lodash functions as ...

Arranging an array using jQuery JSON information

I've been working on sorting an array that is generated by a PHP script. The PHP script retrieves HTML files from a directory and then utilizes jQuery to fetch and display them on the webpage. Below is a snippet of my current code: <script> $( ...

Troubleshooting a UnicodeEncodeError while using the esy.osmfilter package

Encountered an issue with UnicodeEncodeError: 'charmap' codec can't encode characters in position 1-4: character maps to <undefined> when utilizing the esy.osmfilter package (version 1.0.7) to filter an OSM .*pbf file and saving it as ...

Control the switch for CSS selectors

Consider the following scenario where CSS rules are defined: <style> table {background:red;} div {background:green;} </style> In addition, there is HTML code that calls a JavaScript function: <table onclick="tu ...

Create a jQuery popup form with a variety of interactive buttons

Is it possible to implement a modal form in jQuery that can be triggered by multiple buttons? All the buttons have identical names and IDs. The goal is to display the modal form whenever any button is clicked. For this example, let's consider having ...

How come an <a> tag is nabbing mouse clicks from a <canvas> element?

I have been experimenting with creating a 3D piano using three.js. Here is a snippet of the code I am currently working on: let renderer, camera, scene, light, keys, selectedKey; init(); render(); function init() { // Code for initializing renderer, ...

Utilizing Vuex Store in Blade Template | Laravel and Vue.js

I'm trying to figure out how to access a method or variable that is defined in the Vuex 4 store within my Blade file. Currently, I am utilizing a compiled app.js from Vite. While I can easily access the store in Vue.js components, I am curious if it&a ...

Submitting a form using AJAX without specifying the form ID when there is a change

I have a unique setup on my page where a table is created with each cell acting as a form. The goal is for these forms to submit when the input value changes (onchange) without refreshing the entire page. An example scenario would be allowing users to ent ...

I need help figuring out how to showcase the following object in an Angular 5 HTML file

https://i.sstatic.net/XXm3W.png The console screenshot above shows an object with two values: users and tickers, each being an array of values. How can I display these values in an Angular 5 HTML template similar to the screenshot above? I attempted to ...

A guide to identifying a user's browser type using server-side code (Node.js) when making an API request

Can someone help me differentiate between REST API calls made from a desktop browser and those from a mobile browser, specifically tablets? I am currently using Node Express on my server side. ...

Hide the menu by clicking anywhere outside the React component

I'm working on a menu component that needs to close when clicking anywhere on the page while open. Is there a method to achieve this without having to add an event listener to the document and check the event.target? Unfortunately, I cannot send the ...

Executing JavaScript code in the Selenium IDE

I'm having trouble figuring out how to execute JavaScript within the Selenium IDE. The objective is to enter text into an input field, with a backend setup that verifies the current time in the input field for testing purposes: Here's the input f ...

NavigateToTop/BackToTheBeginning

I'm facing a challenge with a page layout that involves two vertical div's - one for the menu on the left and another for loading content on the right. My goal is to utilize the .scrollintoview() function on the menu div so that when a link is cl ...

Several components utilizing a popup module

I have created a modal popup example where scrolling is disabled in the background but enabled within the pop-up itself. Check out my demo here: View Demo My challenge lies in implementing a grid of images on the website: Take a look at the type of grid ...

Increased/Decreased impact

Can someone explain how the "Tell me more" effect is achieved on the website linked below? I'm familiar with the read more/less effect in jQuery, but what intrigues me is that the page cannot be scrolled unless the button is clicked. Here is the link ...