TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

前端 未结 20 1111
时光说笑
时光说笑 2020-12-01 06:22

I am trying to map from a service call but getting an error. Looked at subscribe is not defined in angular 2? and it said that in order to subscribe we need to

20条回答
  •  抹茶落季
    2020-12-01 06:33

    In your example code, you have your map operator receiving two callbacks, when it should only be receiving one. You can move your error handling code to your catch callback.

    checkLogin():Observable{
        return this.service.getData()
                           .map(response => {  
                              this.data = response;                            
                              this.checkservice=true;
                              return true;
                           })
                           .catch(error => {
                              this.router.navigate(['newpage']);
                              console.log(error);
                              return Observable.throw(error);
                           })
       }
    

    You'll need to also import the catch and throw operators.

    import 'rxjs/add/operator/catch';
    import 'rxjs/add/observable/throw';
    

    EDIT: Note that by returning Observable.throwin your catch handler, you won't actually capture the error - it will still surface to the console.

提交回复
热议问题