为什么映射结果未定义?

时间:2017-11-17 15:17:30

标签: json mapping observable angular5

我正在尝试从服务器获取jSon并将其映射到Angular 5应用程序中的类。 以下是无错误工作的服务方法:

getSearchResultTable(hash: number): Observable<Model.SearchResult.RootObject> {

    return this.http.get<Model.SearchResult.RootObject>('http://somelink/api/SearchResult/123')
    .map(res => new Model.SearchResult.RootObject());
    return tmp;
}

以下comoponent类:

export class SearchResultPageComponent implements OnInit {
  searchResult: Model.SearchResult.RootObject;

  constructor(private searchResultService: SearchResultService, private router: Router) { }

  ngOnInit(): void {
    this.searchResultService.getSearchResultTable(122).subscribe( data => {
      this.searchResult = data;
    });
    let t = this.searchResult;
  }
}

Model.SearchResult.RootObject是一个复杂的对象,我是由jSon在following服务上生成的。

在所有示例中都描述了此方法......但在我的情况下,this.searchResultundefined。我做错了什么?

更新

此外,我尝试删除map并以某种方式更改服务方法:

getSearchResultTable(hash: number): Model.SearchResult.RootObject {
this.http.get<Model.SearchResult.RootObject>('http://somelink/api/SearchResult/123')
    .subscribe( data => {
        this.searchResult = data;
      });
    return this.searchResult;
}

和组件方法一样:

  ngOnInit(): void {
    this.searchResult = this.searchResultService.getSearchResultTable(122);
    }

服务获得下一个例外:

  • ReferenceError:未定义数据
  • at eval(eval at SearchResultService.getSearchResultTable(webpack-internal:///../../../../../src/app/search-module/services/search-result.service。 TS), :1:1)
  • 在SearchResultService.getSearchResultTable(webpack-internal:///../../../../../src/app/search-module/services/search-result.service.ts:27: 21)
  • 在SearchResultPageComponent.ngOnInit(webpack-internal:///../../../../../src/app/search-module/pages/search-result/search-result.component。 TS:23:54)
  • at checkAndUpdateDirectiveInline(webpack-internal:///../../../core/esm5/core.js:12291:19)
  • at checkAndUpdateNodeInline(webpack-internal:///../../../core/esm5/core.js:13794:20)
  • at checkAndUpdateNode(webpack-internal:///../../../core/esm5/core.js:13737:16)
  • at debugCheckAndUpdateNode(webpack-internal:///../../../core/esm5/core.js:14609:76)
  • at debugCheckDirectivesFn(webpack-internal:///../../../core/esm5/core.js:14550:13)
  • at Object.eval [as updateDirectives](ng:///SearchModule/SearchResultPageComponent_Host.ngfactory.js:9:5)
  • at Object.debugUpdateDirectives [as updateDirectives](webpack-internal:///../../../core/esm5/core.js:14535:21)

更新

在我的情况下,以下代码段是正确的:

this.http.get<Model.SearchResult.RootObject>('http://someurl/api/SearchResult/123')
.subscribe( (data: Model.SearchResult.RootObject) => {
    this.searchResult = data;
  });
return this.searchResult;

2 个答案:

答案 0 :(得分:0)

在角度5中,您不需要使用地图,您可以在订阅方式中以json形式直接转换您的回复

答案 1 :(得分:0)

让我通过例子解释你: 这是我的服务(Postman)以json格式返回数据。  enter image description here

现在这是我的组件方法 - 调用服务方法的getdata

getdata() {
    this.apiHelperService.getdata().subscribe(domainres => {
      // this.domain = domainres,
      this.msg = JSON.stringify(domainres);//,  
      // this.availabilty = this.domain.Available
    }
      , error => { this.msg = "Error function: " + error; })

  }

现在这是我的服务:

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';

@Injectable()
export class ApiHelperService {
    constructor(private _http: Http) { }

    getdata( ): Observable<any> {      

        return this._http.get("http://localhost:8080/domain/facebookjhjhj.com")
            .map((response: Response) => <any>response.json())
            .do(data => console.log("All: " + JSON.stringify(data)))
            .catch(this.handleError);
    }
    private handleError(error: Response) {
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
    }
}

我在浏览器上得到了这样的结果:

enter image description here

如果我评论map()行,那么我的输出是这样的。

enter image description here

现在回答你的问题看看这一行,你没有将res转换成任何东西,

.map(res => new Model.SearchResult.RootObject());

试试这个:

.map(res => <new Model.SearchResult.RootObject()>response.json());