0

I am trying to write a service for deleting from my external db and I keep seeing this error:

The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. Type argument candidate 'Response' is not a valid type argument because it is not a supertype of candidate 'Response'. Types of property 'type' are incompatible. Type 'string' is not assignable to type 'ResponseType'.

^ and this error highlights the line:

return this.http.delete(`${this.base_url}/${id}`)

Here is the service:

import { Injectable } from '@angular/core';
import { CheckIn } from './check-in';
import { Headers, RequestOptions, Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';


@Injectable()

export class CheckInService {

  private base_url = "http://localhost:3000/";

  constructor (private http: Http) {}

  delete(id:string): Observable<CheckIn[]> {

    return this.http.delete(`${this.base_url}/${id}`)
                    .map((res:Response) => res.json()) .json() on the response to return data
                     .catch((error:any) => Observable.throw(error.json().error || 'Server error')); 
  }
}

What am I doing wrong?

1 Answer 1

1

Try this sample code. I think problem is with the way you are accepting your response.

Main Code

import { Component, OnInit } from "@angular/core";
import {CheckInService} from "../shared/checkin.service";
@Component({
 selector: "test",
 templateUrl: "./test.html",
 styleUrls: ["./test.css"]
})
export class TestComponent implements OnInit{

dataMap:any;
dataDeleteMap:any;
constructor(public dataMapService:CheckInService) {
    this.dataMapService=dataMapService;
}
ngOnInit(){}

  deleteField(id:string){
    this.dataMapService.deleteField(id).subscribe(jsonData=>{
    this.dataDeleteMap=jsonData;
  }); 
 }
}

Service

import { Injectable ,OnInit,OnDestroy} from '@angular/core';
import { Http, Response,Headers,RequestOptions} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import 'rxjs/add/operator/map';

@Injectable()
export class CheckInService {

data:any;
baseUrl:string='http://localhost:3000/';

constructor(public http:Http) { 
}

 deleteField(id:string):Observable<Response>{
   return this.http.delete(`${this.baseUrl}/${id}`)
   .map(result=>result.json());
 }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to implement this, but I'm still not sure what t is supposed to be when I make the call to deleteField()
it was a mistake. t is not needed now you can try the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.