angular - bir servisi çağırma angular2 hatası 'then özelliği Subscription türünde mevcut değil'
login.ts dosyamdan servisi çağırmaya çalışıyorum ama sürekli farklı hatalar alıyorum. İşte kodum
giriş.ts
import { Component } from '@angular/core';
import { Auth, User } from '@ionic/cloud-angular';
import { NavController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { TabsPage } from '../tabs/tabs';
import { AuthService } from '../../services/auth/auth.service';
import {Observable} from 'rxjs/Rx';
@Component({
templateUrl: 'login.html'
})
export class LoginPage {
authType: string = "login";
error: string;
storage: Storage = new Storage();
constructor(public auth: Auth, public user: User, public navCtrl: NavController, public authService: AuthService) {}
facebookLogin() {
this.auth.login('facebook').then((success) => {
this.authService.signup(this.user.social.facebook).then((success) => {
});
});
}
}
auth.service.ts
import { Storage } from '@ionic/storage';
import { AuthHttp, JwtHelper, tokenNotExpired } from 'angular2-jwt';
import { Injectable, NgZone } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { Http, Headers } from 'angular2/http';
@Injectable()
export class AuthService {
jwtHelper: JwtHelper = new JwtHelper();
// contentHeader = new Headers({"Content-Type": "application/json"});
storage: Storage = new Storage();
refreshSubscription: any;
user: Object;
zoneImpl: NgZone;
idToken: string;
error: string;
constructor(private authHttp: AuthHttp, zone: NgZone) {
this.zoneImpl = zone;
// Check if there is a profile saved in local storage
this.storage.get('profile').then(profile => {
this.user = JSON.parse(profile);
}).catch(error => {
console.log(error);
});
this.storage.get('id_token').then(token => {
this.idToken = token;
});
}
public authenticated() {
return tokenNotExpired('id_token', this.idToken);
}
public signup(params) {
var url = 'http://127:0.0.1:3000/api/v1/register';
return this.authHttp.post(url, JSON.stringify(params))
.map(res => res.json())
.subscribe(
data => {this.storage.set('id_token', data.token)},
err => this.error = err
);
}
}
Yani temelde istediğim şey, facebookLogin() tıklandığında auth.service.ts'ye gitmesi ve değeri döndüren singup() metodunu alması.
Ama sürekli olarak Property 'then' does not exist on type 'Subscription'. hatasını alıyorum.
Bu ne anlama geliyor ve bunu nasıl çözebilirim?
Cevap #1
Aboneliği toPromise() olarak değiştirin (toPromise içe aktarmayı unutmayın)
public signup(params) {
var url = 'http://127:0.0.1:3000/api/v1/register';
return this.authHttp.post(url, JSON.stringify(params))
.map(res => res.json())
.toPromise(
data => {this.storage.set('id_token', data.token)},
);
}
veya çağrı yerinde then() yerine subscribe() kullanmak için .map().
public signup(params) {
var url = 'http://127:0.0.1:3000/api/v1/register';
return this.authHttp.post(url, JSON.stringify(params))
.map(res => {
let data = res.json();
this.storage.set('id_token', data.token);
return data;
});
}
facebookLogin() {
this.auth.login('facebook').then((success) => {
this.authService.signup(this.user.social.facebook).subscribe((success) => {
});
});
}
