[英]Property 'subscribe' does not exist on type 'void' in angular 2
getNews(newsType : any){
this.storage.get("USER_INFO").then(right=>{
this.storage.get("sessionkey").then(temp=>{
this.email = JSON.parse(right).email;
this.newkey = temp;
this.authentification =JSON.stringify("Basic " + btoa(this.email+":"+ this.newkey+":"+key));
const body = newsType;
let headers = new Headers({
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': this.authentification
});
let options = new RequestOptions({headers : headers});
return this.http.post('http://api/getNews',body,options)
.map((data:Response) => data.json());
}, err =>{console.log("error on sessionkey",err)})
}, err =>{console.log("error on user",err)})
}
this.httpService.getNews(JSON.stringify(this.category)).subscribe(data => {
this.news = data.News;
});
}, err => {
console.log("Error:", err)
});
I want to call the Api after the success of nested functions. 嵌套函数成功后,我想调用Api。 But when i performing it in the function success callback it it giving me error that Property 'subscribe' does not exist on type 'void'.
但是,当我在函数成功回调中执行它时,它给我错误,类型“ void”上不存在属性“ subscribe”。
How can I return the value of api from service to another .ts file 如何将api的值从服务返回到另一个.ts文件
You're missing return
statement here: 您在这里缺少
return
声明:
getNews(newsType : any){
return this.storage.get('USER_INFO').then(right => {
^^^^^^
However, that would still return a promise with no subscribe
method. 但是,这仍然会返回没有
subscribe
方法的promise。 To wrap a promise result into an observable, you can use from
method: 要将promise结果包装为可观察的结果,可以使用
from
方法:
getNews(newsType : any){
return Observable.from(this.storage.get('USER_INFO').then(right => {
This is the solution which worked for me. 这是为我工作的解决方案。
this.httpService.getNews(JSON.stringify(this.category)).subscribe(data => {
this.news = data.News;
}}, err => {
console.log("Error:", err)
});
getNews(newsType :any) : Observable<any> {
return Observable.create(observer => {
this.storage.get("USER_INFO").then(right=>{
this.storage.get("sessionkey").then(temp=>{
this.email = JSON.parse(right).email;
let authentification =JSON.stringify("Basic " + btoa(this.email+":"+ temp+":"+key));
const body = newsType;
let headers = new Headers({
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': authentification
});
let options = new RequestOptions({headers : headers});
this.http.post('http:api/getNews',body,options).subscribe(data =>{
observer.next(data.json());
observer.complete();
});
}, err =>{console.log("error on sessionkey",err)})
}, err =>{console.log("error on user",err)})
}) }
Thanks @Maximus and @Theophilus Omoregbee 谢谢@Maximus和@Theophilus Omoregbee
link: How to create an observable in Angular 2 链接: 如何在Angular 2中创建可观察对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.