Duhan Uzun
Duhan Uzun

Reputation: 17

"void' is not assignable to parameter of type '(value: Object) => void" in angular, typescript

i cannot solve this problem. i developed web api in .net and i am trying to call api on angular.

//movie.ts 
export class Movie{
    ID: number;
    Title: number;
    GenreId: number;
    Duration: number;
    RatingScore: number;
    PublishDate: Date;
}








//movieservice.ts
import { Injectable, Inject } from "@angular/core";
import {HttpClient} from "@angular/common/http"
import {} from "@angular/core/"
import { Movie } from "../models/movie";

@Injectable({
  providedIn: 'root'
})
export class MovieserviceeService {

  constructor(private httpClient: HttpClient, @Inject("url") private url: string) { }

    GetAll() {
        return this.httpClient.get(this.url + "Movies");
      }
      GetSingle(id: number) {
        return this.httpClient.get(this.url + "Movies/" + id);
      }
      PostAdd(personel: Movie) {
        return this.httpClient.post(this.url + "Movies", personel);
      }
      PutUpdate(personel: Movie) {
        return this.httpClient.put(this.url + "Movies/", personel);
      }
      Remove(id: number) {
        return this.httpClient.delete(this.url + "Movies?id=" + id);
      }
}







//appmovie.component.ts
import { Component, OnInit } from '@angular/core';
import {Movie} from "../../models/movie"
import { MovieserviceeService } from 'src/app/services/movieservicee.service';
;
@Component({
  selector: 'app-appmovie',
  templateUrl: './appmovie.component.html',
  styleUrls: ['./appmovie.component.css']
})
export class AppmovieComponent implements OnInit {
  movieList: Movie[];
  movie: Movie;
  constructor(private MovieserviceeService: MovieserviceeService){}
  ngOnInit(): void {
    throw new Error('Method not implemented.');
  }

  GetAll()
  {
    this.MovieserviceeService.GetAll().subscribe((response: Movie[]) => {this.movieList = response; });
  }

}

i got this error;

No overload matches this call. Overload 1 of 3, '(observer?: Partial<Observer>): Subscription', gave the following error. Type '(response: Movie[]) => void' has no properties in common with type 'Partial<Observer>'. Overload 2 of 3, '(next: (value: Object) => void): Subscription', gave the following error. Argument of type '(response: Movie[]) => void' is not assignable to parameter of type '(value: Object) => void'. Types of parameters 'response' and 'value' are incompatible. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Type 'Object' is missing the following properties from type 'Movie[]': length, pop, push, concat, and 29 more. Overload 3 of 3, '(next?: (value: Object) => void, error?: (error: any) => void, complete?: () => void): Subscription', gave the following error. Argument of type '(response: Movie[]) => void' is not assignable to parameter of type '(value: Object) => void'. Types of parameters 'response' and 'value' are incompatible. Type 'Object' is not assignable to type 'Movie[]'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?error image

Upvotes: 0

Views: 3165

Answers (2)

Felix
Felix

Reputation: 10078

Use generics in the service:

 GetAll() {
        return this.httpClient.get<Movie[]>(this.url + "Movies");
      }

Upvotes: 1

slim
slim

Reputation: 31

This error message occurs when you are trying to pass a function with an incompatible type to an Angular Observable.

To resolve this error, you should check that the function you are passing matches the expected type of the Observable's parameter. In this case, it seems that the Observable expects a function that takes an Object as a parameter, but you are passing a function that takes an array of Movie objects.

To fix this, you can change the type of the function's parameter to Object, or you can change the type of the Observable to expect an array of Movie objects instead of an Object.

Upvotes: 0

Related Questions