4

I'm having a little trouble getting the correct observable type from an http.get request. Here is the function I'm having trouble on:

getMovie(title:string, year:number): Observable<Movie> {
    const params = new HttpParams()
        .set('title', title)
        .set('year', year.toString());
    return this.http.get(this.moviePath, {params})
  }

As you can see I want to receive and Observable of the class Movie from my backend api using the parameters 'title' and 'year'. The problem is that the reponse always comes back with the type Observable<Object> and not Observable<Movie>.

I'm aware that I could create a new Movie instance giving it the properties recieved from the list. The problem with that is that my movie class has properties that are optional. Here is my movie class:

export class Movie {
  title:string;
  year?:number;
  imdb?:string;
  trailer?:string;
  reviewClip?:string;
  reviewSummary?:string;
  reviewScore:number;
  createdAt?:Date;
  bestWeek:boolean;
  bestMonth:boolean;
}

Does anybody know an easy way to get the response to be of the type Observable<Movie> instead of Observable<Object>


Is initial synthesizable in ASIC?

I haven't tried to synthesize myself but if someone could help to give a quick answer, that would be highly appreciated.

For a regular RAM, I see people do this in their Verilog, but I am told that initial is not synthesizable in Synopsys Design Compiler and I wonder if there is a difference among tools. Or this is doable just for memory initialization.

initial begin
    for (count=0;count<2048;count=count+1) RAM[count]=0;
end

This is specifically targeting ASIC instead of FPGA or Xilinx. Thanks in advance!

4

2 回答 2

6

如果你使用HttpClient & Http,它会自动返回 Observable。

所以修改你的代码为

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable'
import { HttpClient,HttpHeaders,HttpResponse,HttpParams } from '@angular/common/http';

import { environment } from '../../../../environments/environment';
import { Movie } from '../models/movie'


@Injectable()
export class MovieService {

  constructor(private http:HttpClient) {

   }

  getMovie(title:string, year:number) {
    const params = new HttpParams()
        .set('title', title)
        .set('year', year.toString());
    return this.http.get<Movie>(this.moviePath, {params})
  }
}

这将为您提供类型为 Movie 的响应

于 2018-02-14T05:06:12.413 回答
3

也许您可以尝试从 json 响应中进行类型转换。

一个例子

public getEmployees(): Observable<IEmployee[]> {  
        return this.http.get(this._repositoryURL)  
            .map((response: Response) =>

   { return <IEmployee[]>response.json() })  
            .catch(this.handleError);  
 }  

你的代码

getMovie(title:string, year:number): Observable<Movie> {
    const params = new HttpParams()
        .set('title', title)
        .set('year', year.toString());
    return this.http.get(this.moviePath, {params}).map((response: Response) => { return <Movie>response.json() })  
                .catch(this.handleError);
 }

 private handleError(errorResponse: Response) {  
        console.log(errorResponse.statusText);  
        return Observable.throw(errorResponse.json().error || "Server error");  
 } 
于 2018-02-14T05:04:21.343 回答