简体   繁体   中英

Displaying user daya after google login ionic

After couple hours making this app, i'm stuck with google login, i've ask question before but still stuck.

so i have logic.service.ts which handle user to login and get their data store them in localStorage

login(){
    this.googlePlus.login({
      'webClientId' : '927898787134-spvfdmvm9apq0e1fo2efvvura8vqpid8.apps.googleusercontent.com',
      'offile' : true
    }).then(res=>{
      firebase.auth().signInWithCredential(firebase.auth.GoogleAuthProvider.credential(res.idToken))
      .then(suc=>{
        // console.log('users', JSON.stringify(suc));
        this.storage.set('user', JSON.stringify(suc));
        this.router.navigate(["/tabs/home"]);
      }).catch(ns=>{
        alert('Unsucessful');
      })
    })
  }

and then i try to get user data and display it on other page, but i'm stuck because i don't get things that i needed. in my home.page.ts

     userData:any;
  loginDetails:any;
  subscribe: any;
  user:any={};
  public items: any;
  constructor(
    private iab: InAppBrowser,
    private router: Router,
    public platform: Platform,
    private service: LogicService,
    private nativeStorage: NativeStorage
    ) {

    this.subscribe = this.platform.backButton.subscribeWithPriority(666666, () => {
      if (this.constructor.name === 'HomePage') {
        if (window.confirm('Do you want to exit the app?')) {
          (navigator as any).app.exitApp();
        }
      }
    });
    let userData = JSON.parse(localStorage.getItem('user'));
    console.log(userData.user);
    console.log(userData.user.displayName);
    console.log(userData.user.email);
    console.log(userData.user.photoURL);
  }
  go() {
      this.router.navigateByUrl('tabs/login');
  }

  ngOnInit(){
    this.service.getLocalData().subscribe(
      data => {this.items = data});
  }
  webview(url){
    this.iab.create(url, '_self', 'location=no,toolbar=no,zoom=no');
  }

i call the data and parse it into object, when i call console.log(user) it shows me this

在此处输入图片说明

but when i try to call user.displayName it says undifined

不明确的

after updated my code, i'm able to log my data but i'm trying to display it on html like this

<div class="header">
          <h3 style="font-weight: bold;">{{ userData.user.displayName }}</h3>
          <p style="color:grey;font-weight:bold;">Get closer with us!</p>
        </div>

but now i get an error like this

在此处输入图片说明

You are accessing the incorrect object, instead use:

let userData = JSON.parse(localStorage.getItem('user'));
console.log(userData.user.displayName);

Also, as mentioned in the comments you need to define userData as a class level so that it is visible in your template (make sure you don't redefine it in the constructor as it will then hide the class-level variable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM