属性 'subscribe' 在类型 'Promise' 上不存在
Property 'subscribe' does not exist on type 'Promise'
我仍然对 rxjs 的工作原理感到困惑。
我正在构建一个向我的服务器发出请求并期望 json 的 Ionic 应用程序。我已经成功订阅了 http.post 并获得了我需要的数据。
但是现在我的问题是我需要在从 Storage 获得的 http 请求中传递一个授权令牌。这是一个问题,因为我需要等到存储准备就绪,然后在调用 http.post 请求之前从中获取令牌值。
这是我试图获取我的 json 数据的地方
getPlanograms() {
//API URL
let requestURL = 'https://myapiurlhere';
let headers = new Headers({'Content-Type': 'application/json'});
return this.storage.ready().then(() => {
return this.storage.get('id_token').then((val) => {
headers.append('Authorization', 'Bearer ' + this.authCredentials.token);
let options = new RequestOptions({headers: headers});
return this.http.post(requestURL, {}, options)
.map(response => <Planogram[]>response.json());
})
});
}
从这里调用
ionViewDidLoad (){
this.merchandisingDataService.getPlanograms()
.subscribe(Planogram => this.planograms = Planogram);
}
但是,当我尝试这样做时,出现以下错误
Property 'subscribe' does not exist on type 'Promise'.
实现我的 objective 的最佳方法是什么?
您可以通过更改 .then()
使用
ionViewDidLoad () {
this.merchandisingDataService.getPlanograms()
.then(Planogram => this.planograms = Planogram);
}
或者,您可以将 getPlanograms
return 设为 Observable
。
getPlanograms() {
// API URL
let requestURL = 'https://myapiurlhere';
let headers = new Headers({'Content-Type': 'application/json'});
// this converts from promise to observable
return Observable.fromPromise(this.storage.ready()
.then(() => this.storage.get('id_token'))
.then((val) => {
headers.append('Authorization', 'Bearer ' + this.authCredentials.token);
let options = new RequestOptions({headers: headers});
return this.http.post(requestURL, {}, options)
// map converts from observable to promise
// (returned by response.json())
.map(response => <Planogram[]>response.json());
});
}));
}
现在您可以像在问题中那样使用 .subscribe()
消费了。
根据 caffinatedmonkey 的建议,我最终得到了这个工作函数:
getPlanograms() {
//API URL
let requestURL = 'https://myapiurlhere';
return Observable
.fromPromise(this.storage.get('id_token'))
.flatMap(token =>{
let headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization', 'Bearer ' + token);
let options = new RequestOptions({headers: headers});
return this.http.get(requestURL, options)
.map(response => <Planogram[]>response.json())
.catch(this.handleError);
}
);
}
这个答案的 2020 / 2021 版本如下,RequestOptions
已弃用,Observable.fromPromise
现在是简单的 from
,flatMap
现在是 mergeMap
你必须 pipe
到 mergeMap:
依赖项/导入项:
import { Observable, throwError, of, from } from 'rxjs';
import { map, catchError, mergeMap } from 'rxjs/operators';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
函数:
listProducts(){
return from(this.storage.get('token'))
.pipe(mergeMap(token =>{
const headers = new HttpHeaders().append('Content-Type', 'application/json').append('Authorization', 'Bearer ' + token);
return this.http.get(`${url}`, { headers: headers })
}
));
}
我仍然对 rxjs 的工作原理感到困惑。
我正在构建一个向我的服务器发出请求并期望 json 的 Ionic 应用程序。我已经成功订阅了 http.post 并获得了我需要的数据。
但是现在我的问题是我需要在从 Storage 获得的 http 请求中传递一个授权令牌。这是一个问题,因为我需要等到存储准备就绪,然后在调用 http.post 请求之前从中获取令牌值。
这是我试图获取我的 json 数据的地方
getPlanograms() {
//API URL
let requestURL = 'https://myapiurlhere';
let headers = new Headers({'Content-Type': 'application/json'});
return this.storage.ready().then(() => {
return this.storage.get('id_token').then((val) => {
headers.append('Authorization', 'Bearer ' + this.authCredentials.token);
let options = new RequestOptions({headers: headers});
return this.http.post(requestURL, {}, options)
.map(response => <Planogram[]>response.json());
})
});
}
从这里调用
ionViewDidLoad (){
this.merchandisingDataService.getPlanograms()
.subscribe(Planogram => this.planograms = Planogram);
}
但是,当我尝试这样做时,出现以下错误
Property 'subscribe' does not exist on type 'Promise'.
实现我的 objective 的最佳方法是什么?
您可以通过更改 .then()
使用
ionViewDidLoad () {
this.merchandisingDataService.getPlanograms()
.then(Planogram => this.planograms = Planogram);
}
或者,您可以将 getPlanograms
return 设为 Observable
。
getPlanograms() {
// API URL
let requestURL = 'https://myapiurlhere';
let headers = new Headers({'Content-Type': 'application/json'});
// this converts from promise to observable
return Observable.fromPromise(this.storage.ready()
.then(() => this.storage.get('id_token'))
.then((val) => {
headers.append('Authorization', 'Bearer ' + this.authCredentials.token);
let options = new RequestOptions({headers: headers});
return this.http.post(requestURL, {}, options)
// map converts from observable to promise
// (returned by response.json())
.map(response => <Planogram[]>response.json());
});
}));
}
现在您可以像在问题中那样使用 .subscribe()
消费了。
根据 caffinatedmonkey 的建议,我最终得到了这个工作函数:
getPlanograms() {
//API URL
let requestURL = 'https://myapiurlhere';
return Observable
.fromPromise(this.storage.get('id_token'))
.flatMap(token =>{
let headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization', 'Bearer ' + token);
let options = new RequestOptions({headers: headers});
return this.http.get(requestURL, options)
.map(response => <Planogram[]>response.json())
.catch(this.handleError);
}
);
}
这个答案的 2020 / 2021 版本如下,RequestOptions
已弃用,Observable.fromPromise
现在是简单的 from
,flatMap
现在是 mergeMap
你必须 pipe
到 mergeMap:
依赖项/导入项:
import { Observable, throwError, of, from } from 'rxjs';
import { map, catchError, mergeMap } from 'rxjs/operators';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
函数:
listProducts(){
return from(this.storage.get('token'))
.pipe(mergeMap(token =>{
const headers = new HttpHeaders().append('Content-Type', 'application/json').append('Authorization', 'Bearer ' + token);
return this.http.get(`${url}`, { headers: headers })
}
));
}