Hello, I'm new here and I'm hoping for some help in simple terms.
I encountered an error in my method sendRequest() while working with the following typescript code...
The error message says: 'Argument of type 'Request' is not assignable to parameter of type 'HttpRequest'. Property 'body' is missing in type 'Request'.
I have attached an image (https://i.sstatic.net/oIP4v.png) of the error message. If anyone knows how to resolve this, please let me know. I couldn't find any solutions on Google.
import { Movie } from "./movie.model";
import { Injectable } from '@angular/core';
import { RequestMethod, Request, Response } from '@angular/http';
import { HttpClient } from '@angular/common/http';
import { Observable } from "rxjs/Observable";
import "rxjs/add/operator/map";
import { Filter } from "./configClasses.repository";
import { Studio } from "./studio.model";
const studiosUrl = "/api/studios";
const moviesUrl = "/api/movies";
@Injectable()
export class Repository {
private filterObject = new Filter();
private movieData: Movie;
constructor(private http: HttpClient) {
//this.filter.category = "drama";
this.filter.related = true;
this.getMovies(true);
}
getMovie(id: number) {
this.sendRequest(RequestMethod.Get, moviesUrl + "/" + id)
.subscribe(response => { this.movie = response.json(); });
//console.log("Movie Data Requested");
}
getMovies(related = false) {
let url = moviesUrl + "?related=" + this.filter.related;
if (this.filter.category) {
url += "&category=" + this.filter.category;
}
if (this.filter.search) {
url += "&search=" + this.filter.search;
}
this.sendRequest(RequestMethod.Get, url)
.subscribe(response => this.movies = response);
}
getStudios() {
this.sendRequest(RequestMethod.Get, studiosUrl)
.subscribe(response => this.studios = response);
}
createMovie(mov: Movie) {
let data = {
name: mov.name, category: mov.category,
description: mov.description, price: mov.price,
studio: mov.studio ? mov.studio.studioId : 0
};
this.sendRequest(RequestMethod.Post, moviesUrl, data)
.subscribe(response => {
mov.movieId = response;
this.movies.push(mov);
});
}
createMovieAndStudio(mov: Movie, stu: Studio) {
let data = {
name: stu.name, city: stu.city, state: stu.state
};
this.sendRequest(RequestMethod.Post, studiosUrl, data)
.subscribe(response => {
stu.studioId = response;
mov.studio = stu;
this.studios.push(stu);
if (mov != null) {
this.createMovie(mov);
}
});
}
replaceMovie(mov: Movie) {
let data = {
name: mov.name, category: mov.category,
description: mov.description, price: mov.price,
studio: mov.studio ? mov.studio.studioId : 0
};
this.sendRequest(RequestMethod.Put, moviesUrl + "/" + mov.movieId, data)
.subscribe(response => this.getMovies());
}
replaceStudio(stu: Studio) {
let data = {
name: stu.name, city: stu.city, state: stu.state
};
this.sendRequest(RequestMethod.Put,
studiosUrl + "/" + stu.studioId, data)
.subscribe(response => this.getMovies());
}
private sendRequest(verb: RequestMethod, url: string,
data?: any): Observable<any> {
return this.http.request(new Request({
method: verb,
url: url,
body: data
})).map(response => {
return response.headers.get("Content-Length") != "0"
? response.json() : null;
});
}
updateMovie(id: number, changes: Map<string, any>) {
let patch = [];
changes.forEach((value, key) =>
patch.push({ op: "replace", path: key, value: value }));
this.sendRequest(RequestMethod.Patch, moviesUrl + "/" + id, patch)
.subscribe(response => this.getMovies());
}
movie: Movie;
movies: Movie[];
studios: Studio[] = [];
get filter(): Filter {
return this.filterObject;
}
deleteMovie(id: number) {
this.sendRequest(RequestMethod.Delete, moviesUrl + "/" + id)
.subscribe(response => this.getMovies());
}
deleteStudio(id: number) {
this.sendRequest(RequestMethod.Delete, studiosUrl + "/" + id)
.subscribe(response => {
this.getMovies();
this.getStudios();
});
}
}