Error: The method map is not a valid function for the HTTP GET operation

In my Angular 4 project, I'm attempting to retrieve data from an API. Following the instructions in this article which outlines the process, but I encountered an error:

TypeError: this.http.get(...).map is not a function

This is the code snippet I'm using:

import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Person } from '../../../interfaces/Person';
import {configuration} from "../config";

@Injectable()
export class AdsService{
  private baseUrl: string = configuration.serverUrl;

  constructor(private http : Http){
  }

  getAll(): Observable<Person[]>{
    let people$ = this.http
      .get(`${this.baseUrl}/people`, {headers: this.getHeaders()})
      .map(mapPeople);

    return people$;
  }
}

function mapPeople(response:Response): Person[]{
  return response.json().results;
}

I would greatly appreciate any assistance!

Answer №1

The map method does not appear to be imported.

Make sure to include this in your imports:

import rxjs/add/operator/map

Answer №2

To utilize the map function in this file, you simply need to include the following line:

import 'rxjs/add/operator/map';

OR

import 'rxjs/add/operators/map';

If you are working with Angular 5 or a newer version, the imports have been updated to :

import { map } from 'rxjs/operator';

OR

import { map } from 'rxjs/operators';

Add this and experience the benefits! :-)

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

Retrieve storage information when an internet connection is unavailable on React Native

One of my recent projects involves creating an application that retrieves data from a URL in the form of an array of objects and then displays this data using FlatList. Upon launching the application, the data is displayed correctly since it's retriev ...

Issue with window resize directive not functioning as expected

I have recently crafted a personalized directive in AngularJS. Here's the code snippet: var esscom = angular.module('esscom',['ngMaterial' ,'ngMessages','ui.bootstrap','ui.router']); esscom.directiv ...

Angular 2 router hybrid application: URL resets after navigation

Each time a route is changed, the correct component is rendered but there seems to be an issue with the path. For example, when navigating from /items to /add-item, the URL changes momentarily but then reverts back. This issue occurs on every page, reg ...

Turn off automatic spelling correction and predictive text in a content-editable section

I'm currently working on a cross-browser application using Script#. I've incorporated a contenteditable div where users can input text. However, I am facing an issue with the auto correct/auto completion feature altering the user's text. Co ...

Verifying the content of the JSON data

If I receive JSON data that looks like this: {"d":1} Is it possible to determine whether the value after "d": is a 1 or a 0? I attempted the following method, but it always goes to the else block, even though I know the JSON data contains a 1. success: ...

Heroku build is reporting that it cannot locate the `@types` in the package.json file

Encountered Heroku Build Error - TSError: ⨯ Struggling to compile TypeScript: - src/server.ts(1,38): error TS7016: File declaration for module 'express' not found. '/app/node_modules/express/index.js' is implicitly of type 'any&a ...

Experiencing problems with various React Carousel libraries when it comes to displaying

I have been attempting to integrate Carousel libraries into my react app, but I am encountering a common issue with all of them. The Carousels show the items on the first slide, but subsequent slides do not display any items, even though they exist within ...

Using Vue.js to dynamically change attribute values based on a condition

I am currently displaying a v-tippy in the following manner: <label v-tippy content="My content text"> Everything is functioning as expected, but now I want to display it based on a condition show_tip == true. Is there a way to achieve th ...

Using JavaScript to display a live data table on a website sourced from Firebase

I am trying to generate a table with data from Google Firebase in JavaScript, but for some reason the table is not being displayed. Below is the code I am using: <a href="#usertable" onclick="openLink(event,'usertable');tab1();" class="tabl ...

Executing a Python function using Javascript

I've been attempting to invoke a basic Python method using JavaScript, but unfortunately, I've hit a roadblock. Below is the Python code snippet I'm working with: def main(): return "Hello from Python" And here is the JavaScript code s ...

Initiating a YouTube video with a click on its thumbnail image - a jQuery tutorial

I am currently working on code that successfully displays YouTube videos. However, I would like the video to start playing when the cover image is clicked. How can I achieve this functionality? Thank you for your help! <div id="video" style="display: ...

Generating a fresh PHP webpage through the administration dashboard

Currently, I am working on developing a blog page using PHP. One key aspect that I am struggling with is figuring out how to efficiently create new posts from the admin panel. Any suggestions or advice would be greatly appreciated! ...

The compatibility issue between Tailwind CSS and Material UI has been identified in the latest version of Next, version 14.0

Currently, I am working with Next 14.0.2 alongside Material UI and Tailwind CSS for my Frontend development tasks. However, I've run into some challenges regarding styling components. One specific issue I faced was when setting a button to navigate to ...

Detecting clicks outside of a component and updating its state using Typescript in SolidJS

Hi there, I am currently learning the SolidJS framework and encountering an issue. I am trying to change the state of an element using directives, but for some reason it is not working. Can anyone point out what I might be doing wrong? You can find the ful ...

An issue occurred while attempting to access the `/blog` page in a next.js project

The Challenge: As I work on developing a blog using Next.js and Sanity, I am facing a browser error when trying to navigate to the /blog route. The specific error message reads as follows: ./sanity.js:2:0 Module not found: Can't resolve '@sanity ...

Values in Local Storage are not located upon first attempt, but after a reload they function properly

import {useEffect} from 'react'; import {useRouter} from 'next/router'; const AuthenticationGuard=props=>{ const {children,fallback} = props; const auth = useAuth(); const router=useRouter(); useEffect(()=>{ if(!r ...

Is there a way to create a universal script that works for all modal windows?

I have a dozen images on the page, each opening a modal when clicked. Currently, I've created a separate script for each modal. How can I consolidate these scripts into one for all modals? <!-- 1 Modal--> <div class="gallery_product col-lg-3 ...

What is causing a single state update when setState is called twice in React?

It seems like I'm making a beginner mistake in React as I am trying to call the "addMessage" function twice within the "add2Messages" function, but it only registers once. I believe this issue might be related to how hooks work in React. How can I mod ...

What are the steps for importing the merge function from RxJS?

When working on my Angular project using version 10, I have found that I can easily import various RxJS operators such as debounceTime, filter, map, and more from 'rxjs/operators'. However, when it comes to importing the merge operator, things ge ...

Next.js does not recognize the _app file

After including the _app.tsx file in my project to enclose it within a next-auth SessionProvider, I noticed that my project is not recognizing the _app.tsx file. Even after adding a div with an orange background in the _app.tsx file, it does not reflect in ...