2017-07-24 39 views
0

更新可觀察到不承認的地圖操作

我注意到一個奇怪的行爲!我在這項服務中有很少的方法,我得到了不同的迴應(一個映射正確,另一個看起來很奇怪)。我更新了代碼。

someService.ts:

 import {Http, Response} from '@angular/http'; 
     import {Observable} from 'rxjs/Observable'; 
     import 'rxjs/add/operator/map'; 

     @Injectable() 
     export class someService { 

      constructor(private _http:Http) { } 

      getSession():Observable<IUser> { // i get a response like that [object object] 
      this.getsession = this._http.get("/url"); 
      return this.getsession.map((response) => <IUser> response.json()); 
      } 

     getUser():Observable<IUser> { // i get the the response object mapped here as one of the comments said the response doesn't have data object 
      this.user = this._http.get("/url"); 
      return this.user.map(response => <User> response.json()); 
     } 
     } 

user.component.ts

import { Component, OnInit } from '@angular/core'; 
import {IUser} from "../common/user"; 
import {SomeService} from "../services/someService.service"; 

@Component({ 
    selector: "user", 
    templateUrl: "./user.component.html", 
}) 
export class User implements OnInit { 
    user:IUser; 

    constructor(private _someService:SomeService) { 
    } 

    getUser() { 
    this._someService.getSession().subscribe((data) => { 
     this.user = data; // the response looks like this: [object Object] 
    }, (err) => { 
     this.errorMsg = "" 
    }); 
    } 
    ngOnInit():void { 
    } 
} 

的JSON對象我從靜止得到我:

{"uid":"***","name":"Test","mail":null,"gname":null,"sn":null,"isEn":true,"user":"**"} 
+3

是什麼「不起作用」是什麼意思? –

+0

@Hazu你在使用地圖時是否遇到任何錯誤? –

+0

@GünterZöchbauer如果我使用地圖操作符,我會得到未定義的地址!沒有映射我得到可觀察的對象 – Hazu

回答

1

嘗試在頂部

添加此導入如果你想所有的運營商

import 'rxjs/Rx'; 

如果你只想在地圖運營商

import 'rxjs/add/operator/map'; 

如果沒有作品請嘗試重新啓動IDE

+1

如果你導入'rxjs/Rx',你會得到**全部**,所以第二次導入是多餘的。 – cartant

+0

抱歉,我應該添加一個或我將更新我的答案 –

+0

第二行與問題中顯示的完全相同,因此這不太可能是解決方案。 –