The function of TypeScript map is not working properly

Encountering the error message "data.map is not a function" while trying to map data from a REST API request returning JSON data. It appears that the issue may stem from the data structure, as it seems like the returned data should be accessed with data.data instead of data. Any suggestions on how to properly handle this mapping would be greatly appreciated.

Take a look at the JSON Data Structure.

Below is the service code snippet:

  getAlladvisors$ = this.http.get<IAdvisor[]>("http://localhost:8055/items/advisor")
    .pipe(
      map((data: IAdvisor[]) =>
    data.map(
      a =>
      ({
        name: a.name,
        id: a.id,
        page: a.page,
        multilang: a.multilang
      })
    )
  ),

AdvisorsandQuestions$ = this.http.get<IAdvisorsAndQuestions[]>("http://localhost:8055/items/question")
  .pipe(
    tap(data => console.log("Questions IDs", data))
  );
    
getAdvisorsWithId$ = combineLatest([
      this.getAlladvisors$,
      this.AdvisorsandQuestions$
    ]).pipe(
      map(([product, categories]) =>
        product.map(product => ({
          ...product,
          questionId: categories.find(c => product.id === c.id).questions_Id,
        }) as IAdvisor)
      ),
    );

Here is the Interface definition:

export interface IAdvisor {
  id: number,
  name: string,
  page: string,
  multilang: boolean  
}

Answer №1

My thoughts on the answer regarding the data: <IAdvisor []> suggest that it may simply represent some form of data.

  retrieveAllAdvisors$ = this.http.get<IAdvisor[]>
       ("http://localhost:8055/items/advisor")
       .pipe(
            map((data: <IAdvisor[]>) => {
              if (data) {
                console.log(data);
              }
              return data;
            }),
            catchError((error) => {
                 console.log(error)
           }
         })

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

Efficiently handling heavy components in Angular using ngFor

Encountered an issue: I have an array containing chart configurations that need to be displayed. Currently, I am iterating through the array and rendering the charts as shown below: <ng-container *ngFor="let config of configs; trackBy: getId"& ...

What is the best way to use Immer to update Zustand state when incorporating objects that are added through a controlled form using React-Hook-

Having some trouble with integrating Zustand and Immer using React-Hook-Form. My goal is to capture a series of values from a form, store them in a list, and allow for the addition of new objects to that list. In this scenario, the user inputs data for a ...

Exploring the Power of Combining Reducers with Angular 6 and NGRX

I am currently working with Angular 6 and NgRX 4, trying to combine multiple reducers. app.module.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { StoreModule ...

Exploring the DynamoDB List Data Type

Currently, I am working on an angular 8 application where I have chosen to store JSON data in a list data type within DynamoDB. Inserting records and querying the table for data has been smooth sailing so far. However, I have run into some challenges when ...

The property slider in the d3 slider package is not found in the type 'types of d3'

I attempted to integrate a d3 slider into my d3 chart in Angular 2. I installed the d3slider package using the command: npm install --save @types/d3.slider. However, when trying to access the method "d3.slider()", an error occurred stating that "property ...

Understanding TypeScript's ability to infer types in generics

Exploring the world of TypeScript through a robustly typed system for REST requests. Let's dive into the code: This type is used to establish the connection between routes and their respective object types: export interface RoutesMapping { api1: ...

"encountered net::ERR_NAME_NOT_RESOLVED error when trying to upload image to s3 storage

I am currently developing an application using Angular. I have been attempting to upload a picture to my S3 bucket, but each time I try, I encounter this error in the console. https://i.stack.imgur.com/qn3AD.png Below is the code snippet from my upload.s ...

Can components be SSGed individually rather than entire pages?

I am currently working with Next.js and I am wondering if there is a way to statically generate and display the database values in the header and footer components used across all pages. While getStaticProps can generate pages statically, it doesn't ...

What is the best way to specify the data type of a value within a map in TypeScript?

I need assistance defining the value of a key in a map as a key-value pair in TypeScript. map: { key: someStruct } Is it possible to declare the type of someStruct and initialize it simultaneously? What is the best approach for accomplishing this? ...

Strategies for eliminating nested subscriptions in the search for names

I need assistance with refactoring a component I created to search for GitHub users by login. The current implementation contains nested subscribe blocks, and I would like to rewrite it using rxjs operators without nesting them. You can find the live exam ...

Guide to aligning a fraction in the center of a percentage on a Materal Design progress bar

Greetings! My objective is to create a material progress bar with the fraction displayed at the top of the percentage. Currently, I have managed to show the fraction at the beginning of the percentage. Below is the code snippet: <div class=" ...

A step-by-step guide for updating a minor version of Angular with Angular CLI

I've been searching online for the answer to this straightforward question, but can't seem to find it anywhere... In my angular 4 project (made with angular cli), I want to utilize the newly introduced http interceptors in version 4.3. Could so ...

Steps to modify the CSS of a custom component in Angular 8

I have been attempting to override the css of a custom component selector, however, my attempts have been unsuccessful. I have tried using ":ng-deep" but it hasn't worked. How can I go about finding a solution for this issue? app.component.html: < ...

Exploring Angular 2: How to Retrieve the Value of a Radio Button

How can I retrieve the value of the radio button that is clicked in app.component.html from within app.component.ts? app.component.html <div class="container"> <div class="row"> <div class="col-sm-3 well" style="width: 20%"> ...

Is there a way to import TypeScript modules from node_modules using browserify?

After successfully running tsc, I am facing difficulty understanding how to import TypeScript modules from node modules. The crucial section of my gulp file is as follows: gulp.task('compile-ts', ['clean'], function(){ var sourceTsF ...

What is the best approach to creating multiple dropdowns in ant-design with unique options for each?

It seems like I may be overlooking a simple solution here. Ant-Design dropdowns utilize an array of ItemProp objects to show the options, but this restricts me to having only one list of options. const choices: MenuProps['items'] = [ { label: ...

Guide on retrieving Images from an express API using mongo DB

My API includes the quoteImage key which contains the path to the image stored in the backend. If you want to see the image, click here: API image I am currently working on my react application and struggling to display the images despite being able to sh ...

Issues installing dependencies in Angular using npm

Today I attempted to create a new project. I used the following command: ng new NAME --style=less However, when I ran this command in my cmder, it resulted in errors and warnings. To troubleshoot, I decided to uninstall Node.js along with the files in R ...

Create a simulated class to serve as a property within a service

Currently, I'm working on an Ionic application and have created an AlertService class with two properties: messageAlert: Alert; errorAlert: Alert; The Alert class belongs to the Ionic framework, so I cannot modify it. To mock the Alert class, I came ...

Incorporate an external library

I am currently facing a challenge in my angular2 project where I need to import a 3rd party library. Here are the steps I have taken so far: ng new myproject npm install --save createjs-easeljs npm install @types/easeljs However, I am stuck at this poin ...