Javascript 2中的类型“void”上不存在属性“subscribe”
我想在嵌套函数成功后调用Api。 但是,当我在函数success回调中执行它时,它会给我一个错误,即类型“void”上不存在属性“subscribe” 如何将api的值从服务返回到另一个.ts文件此处缺少返回语句:Javascript 2中的类型“void”上不存在属性“subscribe”,javascript,angular,typescript,ionic2,angular2-services,Javascript,Angular,Typescript,Ionic2,Angular2 Services,我想在嵌套函数成功后调用Api。 但是,当我在函数success回调中执行它时,它会给我一个错误,即类型“void”上不存在属性“subscribe” 如何将api的值从服务返回到另一个.ts文件此处缺少返回语句: getNews(newsType : any){ this.storage.get("USER_INFO").then(right=>{ this.storage.get("sessionkey").then(temp=>{ this.email = JSON.p
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)
});
然而,这仍然会返回一个没有subscribe方法的承诺。要将承诺结果包装为可观察结果,可以使用from方法:
这就是对我有效的解决方案
getNews(newsType : any){
return Observable.from(this.storage.get('USER_INFO').then(right => {
谢谢@Maximus和@Theophilus Omoregbee
Link:
AS @ Maximus说,你必须在嵌套函数内返回父级外的数据,返回这个.SturaG.GuuleSeri-IfFoEE,如果它有帮助,请考虑接受。getNews(newsType : any){
return Observable.from(this.storage.get('USER_INFO').then(right => {
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)})
}) }