简体   繁体   English

Angular 4 HttpClientModule 和 map() 函数

[英]Angular 4 HttpClientModule and map() function

I am facing an issue in regard to new HttpClientModule and the map() function from RX.js.我正面临一个关于新的HttpClientModule和来自 RX.js 的map()函数的问题。

What I want to do is to change the objects of the returned array inside the observable coming from the get() method.我想要做的是更改来自get()方法的 observable 中返回数组的对象。

My curent code:我当前的代码:

 get(url: string): Observable < any > { return this.http.get(this.config.baseUrl + url); } playerSearch(text: string): Observable < any[] > { if (text == "" || !text) { return Observable.of([]); } else { return this.auth.get(`/players?$expand=Contact($expand=Blob)&$filter=contains(Contact/LastName, '${text}') or contains(Contact/FirstName, '${text}')`).map((x) => { return { Id: x.Id, Name: x.Contact.FirstName + " " + x.Contact.FatherName.substring(0, 2) + ". " + x.Contact.LastName, BlobSrc: this.utilitiesService.imageLinkCreator(x) } }); } } search = (text$: Observable < string > ) => text$ .debounceTime(300) .distinctUntilChanged() .do(() => this.searching = true) .switchMap(term => this.dataService.playerSearch(term) .do(() => this.searchFailed = false) .catch(() => { this.searchFailed = true; return Observable.of([]); })) .do(() => this.searching = false);

The error that I get:我得到的错误:

Type 'Observable<{ Id: any;输入'Observable<{ Id: any; Name: string;名称:字符串; BlobSrc: string; BlobSrc:字符串; }>' is not assignable to type 'Observable'. }>' 不可分配给类型 'Observable'。

Type '{ Id: any;输入 '{ ID: any; Name: string;名称:字符串; BlobSrc: string; BlobSrc:字符串; }' is not assignable to type 'any[]'. }' 不可分配给类型 'any[]'。

As far as I know, the map() method returns an observable with value only one object instead of an array.据我所知, map()方法返回一个只有一个对象而不是数组的observable

What is the correct syntax in order to return an observable containing an Array of { Id: any; Name: string; BlobSrc: string; }返回包含{ Id: any; Name: string; BlobSrc: string; }数组的 observable 的正确语法是{ Id: any; Name: string; BlobSrc: string; } { Id: any; Name: string; BlobSrc: string; } { Id: any; Name: string; BlobSrc: string; } objects? { Id: any; Name: string; BlobSrc: string; }对象?

If your http request returns an array of players, then this is how your code should look like:如果您的 http 请求返回一个玩家数组,那么您的代码应该如下所示:

get(url: string): Observable<any[]> {
        return this.http.get(this.config.baseUrl + url);
    }


playerSearch(text: string): Observable<any[]> {
    if (text == "" || !text) {
        return Observable.of([]);
    } else {
        return this.auth.get(`/players?$expand=Contact($expand=Blob)&$filter=contains(Contact/LastName, '${text}') or contains(Contact/FirstName, '${text}')`).map((arr: any[]) => {
            return arr.map(x => ({
                Id: x.Id,
                Name: x.Contact.FirstName + " " + x.Contact.FatherName.substring(0, 2) + ". " + x.Contact.LastName,
                BlobSrc: this.utilitiesService.imageLinkCreator(x)
            });
        });
    }
}
playerSearch(text: string): Observable<any[]> {
    if (text == "" || !text) {
        return Observable.of([]);
    } else {
        return this.auth.get(`/players?$expand=Contact($expand=Blob)&$filter=contains(Contact/LastName, '${text}') or contains(Contact/FirstName, '${text}')`)
            .map((arr: any[]) => {
                return arr.map(x => {
                    debugger;
                    ({
                        Id: x.Id,
                        Name: x.Contact.FirstName + " " + x.Contact.FatherName.substring(0, 2) + ". " + x.Contact.LastName,
                        BlobSrc: this.utilitiesService.imageLinkCreator(x)
                    })
                });
            });
    }
}

Here is the code I try to run, based on yours Amit.这是我尝试运行的代码,基于您的 Amit。 I add a debugger inside nested map but never get in. There is no actual error.我在嵌套map添加了一个调试器,但从未进入。没有实际错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM