The Observable<ArrayBuffer> type does not have a property map

I encountered an issue while upgrading from Angular v4 to Angular v6. I was in the process of replacing Http and HttpModule with HttpClient and HttpClientModule. As a result, I imported HttpClient from @angular/common/http in a service to fetch results from DBpedia's API. Previously, I utilized Http from @angular/http and my code functioned correctly.

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Jsonp, Headers, RequestOptions, URLSearchParams} from "@angular/http";
import {Store} from "@ngrx/store";
import * as fromRoot from '../reducers';
import {Observable} from "rxjs";

@Injectable()
export class KnowledgeapiService {

  server = 'http://lookup.dbpedia.org';
  searchURL = this.server + '/api/search/KeywordSearch?';
  homepage = 'https://susper.com';
  logo = '../images/susper.svg';

  constructor(
    private http: HttpClient,
    private jsonp: Jsonp,
    private store: Store<fromRoot.State>
  ) {}

  public getsearchresults(searchquery){
    let params = new URLSearchParams();

    params.set('QueryString', searchquery);

    let headers = new Headers({ 'Accept': 'application/json' });
    let options:any = new RequestOptions({ headers: headers, search: params });

    return this.http
      .get(this.searchURL, options).map(res =>

        res.json()

      ).catch(this.handleError);
  }

  private handleError (error: any) {
    // In some advance version we can include a remote logging of errors
    let errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // Right now we are logging to console itself
    return Observable.throw(errMsg);
  }
}

However, after switching from using Http to HttpClient, I encountered an error in the getsearchresults(searchquery) function. Whenever I tried to use the map function to map data to JSON, it threw an error stating that the map function does not exist on type Observable<ArrayBuffer>. Removing the map function resulted in a similar message for the catch function. I referred to https://github.com/angular/angular/issues/15548 but the suggested solution did not work for me. Can someone point out where I might be going wrong? Should I consider removing both the map and catch functions in getsearchresults(searchquery)?

Answer №1

It seems that the issue you are encountering is related to the inconsistency in defining types within the TypeScript code provided. For instance, there is no type specified for the getsearchresults() function, as well as in the

this.http.get<NoTypeDefinedHere>
statement.

In addition, it is worth mentioning that the httpClient module normally returns JSON responses by default, making the creation of new headers with

let headers = new Headers({ 'Accept': 'application/json' });
unnecessary in this case.

Please verify whether you are utilizing the http module or the httpClient module in your imports.

Also, double-check if the import statement for rxjs/add/operator/map is correctly included in your project.

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

What is the process for implementing a decorator pattern using typescript?

I'm on a quest to dynamically create instances of various classes without the need to explicitly define each one. My ultimate goal is to implement the decorator pattern, but I've hit a roadblock in TypeScript due to compilation limitations. Desp ...

I am encountering difficulties when attempting to install a specific Angular project using npm install

My old laptop was able to run my Angular project smoothly, but now I'm encountering numerous errors when trying to install it with npm on a new device. Any assistance based on the attached log would be greatly appreciated. Thank you in advance. shiva ...

I have to create a duplicate for the clipboard containing a dynamic variable in Angular

CSS Note: The Technical.url variable in the specification is constantly changing, and every time I click the button, I want to copy the URL. <div fxLayout="column" fxLayoutAlign="center start" fxFlex="70" class="" ...

What is the best way to showcase information in Angular?

I am facing an issue where my cards are not displaying data from the database, they appear empty. Can anyone provide a solution to help me fix this problem? Below is the output I am getting, with the empty cards that I want to populate with data. https:// ...

What is the correct way to incorporate service within a component function?

Here's how I'm implementing an inline input edit component: <app-inline-input-edit [label]="'Manufacturer'" [required]="true" [(ngModel)]="ctrlTypes.manufacturer" name="manufacturer" [changed]="onChange"> ...

I'm at a loss as to why the NestJS provider is showing as undefined in my code

Prisma.service.ts import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common' import { PrismaClient } from '@prisma/client' @Injectable() export class PrismaService extends PrismaClient implements OnModuleInit, OnMod ...

When using RxJS forkjoin, it will cease subscription if there is a flatMap present within one of the observables it is awaiting

I have been experimenting with nested calls using rxjs and trying to implement nested forkJoins. However, I have encountered an issue where the outer forkJoin stops returning a result when there is a flatMap inside the inner observable. Here is a snippet ...

The mapStateToProps function in a Higher Order Component is receiving an OwnProps argument with a property that is not defined

I'm a new user of react-redux trying to connect a higher-order component to the react-redux store. In the mapStateToProps function, I'm using the ownProps argument to filter the state. However, the property I'm trying to use within OwnProps ...

UI components displaying varying data retrieved from Redux, despite having identical user interfaces

Currently, I have a component that receives data from the Redux store using useSelector. The UI remains the same, but I need to change where the data is coming from by passing the selector through props. My first question is: Is it acceptable to pass the ...

Angular routing unit testing: Breaking down routing testing into individual route testing sequences

Currently, I am in the process of testing the routing functionality of my Angular application: Below is the file where I have declared the routes for my app: import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@ ...

Modifying audio output in a React element

I am trying to incorporate background music into my React app using TypeScript. However, I am encountering an issue where changing the music in the parent component does not affect the sound playing in the child node. import React from 'react'; ...

Move your cursor over a div element while utilizing NgClass

I'm currently working on implementing a hover effect on a div using an Angular ngClass directive. Within the template file, I have a div element with the class "list-item-container" that includes another div with the class "list-item." This "list-item ...

The Children Element in Next.js and React Context with TypeScript is lacking certain properties

Encountering a mysterious error while trying to implement React's Context API in Next.js with TypeScript. The issue seems to be revolving around wrapping the context provider around the _app.tsx file. Even though I am passing values to the Context Pr ...

Exploring the Power of Modules in NestJS

Having trouble with this error - anyone know why? [Nest] 556 - 2020-06-10 18:52:55 [ExceptionHandler] Nest can't resolve dependencies of the JwtService (?). Check that JWT_MODULE_OPTIONS at index [0] is available in the JwtModule context. Possib ...

Tips for effective page management

After creating a navbar in my project, I've come to the realization that it requires a component for each page and subpage. This seems redundant especially when dealing with multiple navigation options like shown in this image. Is it necessary to crea ...

Run JavaScript code whenever the table is modified

I have a dynamic table that loads data asynchronously, and I am looking for a way to trigger a function every time the content of the table changes - whether it's new data being added or modifications to existing data. Is there a method to achieve th ...

What is the best way to retrieve a property from a conditional type using generics?

The code snippet above presents an issue in TypeScript: const exampleFn = function<AttributeName extends 'attributeA' | 'attributeB'>( whatToProcess: AttributeName extends 'attributeA' ? {attributeA: string} : {attri ...

Access the properties of the encapsulated component in Vue 3, allowing for IDE autocomplete support

I have a vue3 component named MyButton which acts as a wrapper for the vuetify v-btn component. I am trying to figure out a way to make MyButton props inherit all of the props that v-btn has and also enable autocomplete feature in IntelliJ or VSCode. Is it ...

How can I pass Checkbox values to a function in the component in Angular 2?

Hello, I am new to Angular and currently working with NgFor. I have a requirement where I need to display elements that can be selected by the user. Upon selection, I should be able to retrieve the ID in the Component function. However, when I tried usin ...

Creating custom observables by utilizing ViewChildren event and void functions in Angular 12: A step-by-step guide

I am currently working on developing a typeahead feature that triggers a service call on keyup event as the user types in an input field. The challenge I face is that my input field is enclosed within an *ngIf block, which requires me to utilize ViewChildr ...