2017-04-20 45 views
2

我是離子2的新用戶,並且存儲我的用戶數據時存在問題,該數據存儲在我的注射服務中的類中以供登錄請幫助我:如何使用ionic2框架登錄後如何存儲用戶ID

AUTH-service.ts

export class User { 
    nom: string; 
    email: string; 

    constructor(nom: string, email: string) { 
    this.nom = nom; 
    this.email = email; 
} 
} 
    @Injectable() 
    export class AuthService { 
    currentUser: User; 
    members: Array<any>; 
    nomUser:string; 
    emailUser:string; 

     constructor(public http: Http) { 
       console.log('Hello Authentification'); 
      } 

     public login(credentials) { 
     if (credentials.email === null || credentials.password === null) { 
     return Observable.throw("Please insert credentials"); 
     } else { 
     return Observable.create(observer => { 
     var url = 'http://localhost/PFEBACKEND/login-client.php? 
     email='+credentials.email+'&passe='+credentials.password ; 

     this.http.get(url).map(res => res.json()).subscribe(
      data=>{                  
       let access = (data.error=== 0) 

       this.currentUser = new User(data.nom,data.email); 

       console.log(this.currentUser); 
        observer.next(access); 
        observer.complete(); 
        }, 
        err => { 
        console.log(err); 
        }, 
        () => console.log('authentifié')); 

       }); 
    } 

      public getUserInfo() { 
      return this.currentUser ; 
      } 

home.ts

import { AuthService } from '../../providers/auth-service'; 
import { Http, Headers, RequestOptions } from '@angular/http'; 
import {Observable} from 'rxjs/Observable'; 
import { Component,Inject } from '@angular/core'; 
import { NavController, NavParams,LoadingController } from 'ionic-angular'; 

    export class HomePage { 
      nom:string= ''; 
      email:string= ''; 

    constructor(public http: Http,public navCtrl: NavController, private 
    auth:AuthService,public data:Datamembers, public navParams:NavParams, 
    public loadingCtrl:LoadingController) 
    {  
    let info = this.auth.getUserInfo(); 
    this.email = info.email; 
    this.nom = info.nom; 
    } 
    } 

錯誤:

nav-controller-base.js:88 Failed to navigate: Cannot read property 'email' of undefined (anonymous) @ nav-controller-base.js:88
core.es5.js:1085 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'email' of undefined

回答

0

除了這個,你已經獲得了絕大部分的權利 - 你不能直接從一個observable到一個方法的調用類中返回數據。您需要傳遞該可觀察值。在調用類中解析它。讓我爲你糾正這個問題。

return Observable.create(observer => { 
    var url = 'http://localhost/PFEBACKEND/login-client.php?email='+credentials.email+'&passe='+credentials.password ; 

this.http.get(url).map(res => res.json()).subscribe(
    data=>{                  
     let access = (data.error=== 0) 

     this.currentUser = new User(data.nom,data.email); 

     console.log(this.currentUser); 
     observer.next(this.currentUser); 
     observer.complete(); 
    }, 
    err => { 
     console.log(err); 
     observer.error(err); 
    }, 
    () => console.log('authentifié') 
); 

而且在home.ts

this.auth.getUserInfo().subsribe((data) => { 
    this.email = data.email; 
    this.nom = data.nom; 
},(err) => { 
    console.log("Error :",err); 
}); 

注:在這裏,我假設this.currentUser, access對你很重要。如果他們不是,最後如果您需要home.ts中的所有內容,則只需刪除這兩個語句,然後在那裏執行observer.next(data);

+0

謝謝soooo多!!! @sagar它的工作,但我在如何保存用戶和他們的信息後登錄??鬥爭我與clas工作s在我的身份驗證服務中將Username命名爲User,但是一旦我刷新頁面,所有的信息都已消失 –

+0

您想在哪裏存儲它?無論如何,這個問題得到了回答。很高興幫助。 :) –

+0

我有一個數據庫,但我不知道什麼是最好的登錄信息!!也許本地存儲!你怎麼想 ? –

1

您需要resolveemailpromise內,如下圖所示。

this.auth.getUserInfo().then((i) => { 
    this.email = i.email; 
    this.nom = i.nom; 
    } 
+0

我的功能** getUserInfo **返回一個包含「email」和「nom」的用戶對象 public getUserInfo():用戶{return}返回this.currentUser; } **我在我的服務中有一個名爲User的類**: export class User { nom:string; email:string; 構造函數(nom:string,email:string){this.nom = nom; this.email = email; (**)導致getUserInfo()只包含「nom」和「email」!!請幫助我!! –

+0

我在home.ts中做了這個* * this.mail = this.auth.getUserInfo()。email; ** ** this.nom = this.auth.getUserInfo()。nom; **當我試圖在home.html ** { {email}} **我得到了同樣的錯誤 –

+1

你可以在你的原始問題中加上'getUserInfo()'的詳細信息嗎?不要把它放在評論區域。不能讀取它。 – Sampath

相關問題