1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Argument of type 'Request' is not assignable to parameter of type 'HttpRequest<any>'

Discussão em 'Angular' iniciado por myfunnyfella, Setembro 17, 2024.

  1. myfunnyfella

    myfunnyfella Guest

    I'm new. Please be gentle and speak layman.

    In the following typescript code, I'm getting this error in my method sendRequest()...

    Argument of type 'Request' is not assignable to parameter of type 'HttpRequest'. Property 'body' is missing in type 'Request'.


    I attached an image of the message. Anyone know how to fix this please? No luck 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();
    });
    }
    }


    [​IMG]

    Continue reading...

Compartilhe esta Página