Angular 2订阅无效

时间:2017-02-23 09:54:30

标签: angular google-app-engine google-app-engine-python

我有角度2的服务

private todosUrl = "http://localhost:3000/HelloWorld";  // URL to web API

  constructor (private http: Http) {}

  public getTodos(): Subscription {
    this.todoList = [];
    return this.http.get(this.todosUrl)
        .map((res: Response) => {
            if (res.status === 204) {
                return [];
            }
            let todosObj: any = res.json();

            return todosObj;
        })
        .flatMap((res: Array<Todo>) => {
          return res;
        })
        .subscribe((todo: Todo) => {
          this.todoList.push(todo);
        });
  }

http://localhost:3000/HelloWorld返回一个json {“Hello World!”:“立即尝试”}。但是这个函数返回错误

**Uncaught TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
    at Object.subscribeToResult (subscribeToResult.ts:81)
    at MergeMapSubscriber._innerSub (mergeMap.ts:135)
    at MergeMapSubscriber._tryNext (mergeMap.ts:131)
    at MergeMapSubscriber._next (mergeMap.ts:115)**

有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

private todosUrl = "http://localhost:3000/HelloWorld";  // URL to web API

  constructor (private http: Http) {}

  public getTodos(): Subscription {
    this.todoList = [];
    return this.http.get(this.todosUrl)
        .flatMap((res: Response) => {
            if (res.status === 204) {
                return null;
            }
            return res.json();
        }).subscribe((todo: Todo) => {
          this.todoList.push(todo);
        });
  }
相关问题