错误TypeError:您在期望流的位置提供了“ undefined”。您可以在Angular Services中提供Observable,Promise,Array或Iterable

时间:2019-08-07 11:37:11

标签: angular typescript rxjs

我编写了多种服务来使用不同的API。为帖子编写的服务以某种方式工作但出现此错误

  

错误TypeError:您在期望流的位置提供了“ undefined”。您可以提供一个Observable,Promise,Array或Iterable。       在subscriptionTo(subscribeTo.js:27)

对于为GET方法编写的服务,将显示相同的错误,但我没有得到任何数据作为回报。 这是我的服务

GetClientData(): Observable<ClientDto> {
    let url_ = this.baseUrl + "https://xxxxxxxxxxxxxxxx/getClients";
    url_ = url_.replace(/[?&]$/, "");

    let options_ : any = {
        observe: "response",
        responseType: "blob",
        headers: new HttpHeaders({
            "Accept": "application/json"
        })
    };

    return this.http.request("get", url_, options_).pipe(_observableMergeMap((response_ : any) => {
        return this.processGetAllPermissions(response_);
    })).pipe(_observableCatch((response_: any) => {
        if (response_ instanceof HttpResponseBase) {
            try {
                return this.processGetAllPermissions(<any>response_);
            } catch (e) {
                return <Observable<ClientDto>><any>_observableThrow(e);
            }
        } else
            return <Observable<ClientDto>><any>_observableThrow(response_);
    }));
}

protected processGetAllPermissions(response: HttpResponseBase): Observable<ClientDto> {
    const status = response.status;
    const responseBlob = 
        response instanceof HttpResponse ? response.body : 
        (<any>response).error instanceof Blob ? (<any>response).error : undefined;

    let _headers: any = {}; if (response.headers) { for (let key of response.headers.keys()) { _headers[key] = response.headers.get(key); }};
    if (status === 200) {
        return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
        let result200: any = null;
        let resultData200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
        result200 = resultData200 ? ClientDto.fromJS(resultData200) : new ClientDto();
        return _observableOf(result200);
        }));
    } else if (status !== 200 && status !== 204) {
        return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
        return throwException("An unexpected server error occurred.", status, _responseText, _headers);
        }));
    }
    return _observableOf<ClientDto>(<any>null);
}

云功能

exports.getClients = functions.https.onRequest((req, res) => {

    res.set('Access-Control-Allow-Origin', 'http://localhost:4200');
    res.set('Access-Control-Allow-Methods', 'GET', 'POST');
    res.set('Access-Control-Allow-Headers', 'Content-Type');

    if (req.method === 'OPTIONS') {
        res.end();
    }

    else
    {

        let allClients = [];
        usersClients.get()
            .then(snapshot => {
                snapshot.forEach(doc => {
                    allClients.push(doc.data());
                });
                res.send(200, {
                    "data": allClients
                })
            })
            .catch(err => {
                console.log("Error getting data", err);
            });
    }
});

1 个答案:

答案 0 :(得分:1)

遵循以下原则:

return this.http.get(url_, options_).pipe(
  map(data => {
    // Transform data here
  }),
  catchError(err => {
    // Handle errors here
  })
);