未捕获(承诺):TypeError:无法读取属性' apply'未定义的离子2应用程序

时间:2017-01-13 00:15:57

标签: javascript facebook angular ionic-framework ionic2

我试图进行原生facebook登录,但我收到此错误

  

未捕获(承诺):TypeError:无法读取属性' apply'未定义的

enter image description here

我告诉你我所有的项目代码。 只是说它让我成功连接到facebook,但它向我显示了这些错误。

"$OUTPUT/stage2_ic00${n}_${cond}.nii.gz"

LoginPage.ts

import { Component, ViewChild } from '@angular/core';
import { Platform, Nav } from 'ionic-angular';
import { StatusBar, Splashscreen, NativeStorage } from 'ionic-native';

import { LoginPage } from '../pages/login/login';
import { UserPage } from '../pages/user/user';

@Component({
  template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {

  @ViewChild(Nav) nav: Nav;
  rootPage: any;

  constructor(platform: Platform) {
    platform.ready().then(() => {

      // Here we will check if the user is already logged in
      // because we don't want to ask users to log in each time they open the app
      let env = this;
      NativeStorage.getItem('user')
      .then( function (data) {
        // user is previously logged and we have his data
        // we will let him access the app
        env.nav.push(UserPage);
        Splashscreen.hide();
      }, function (error) {
        //we don't have the user data so we will ask him to log in
        env.nav.push(LoginPage);
        Splashscreen.hide();
      });

      StatusBar.styleDefault();
    });
  }
}








/*import { Component,ViewChild } from '@angular/core';
import { Platform,Nav, LoadingController} from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';

//import pages
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
import { SignupPage } from '../pages/signup/signup';
import { TabsPage } from '../pages/tabs/tabs';
import { AboutPage } from '../pages/about/about';
import {StudentsPage} from '../pages/students/students';



//import plugins
import firebase from 'firebase';
//import {AngularFire, FirebaseListObservable} from 'angularfire2';

@Component({
  template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;
   public rootPage:any;
   //ref firebase
  public semesters:any;
  public userProfile: any;
  //user variables
  public currentUser: any;
  public id_user:any;
  public reg_boolean:any;
  public department:any;
  public year:any;
  public semester:any;
  public semesterdetails:any;

  constructor(platform: Platform, public loadingCtrl: LoadingController) {
    firebase.initializeApp({
    apiKey: "AIzaSyDOpRshkjuy_tGXSzw34RFHXmAFelyAUt0",
    authDomain: "haversami.firebaseapp.com",
    databaseURL: "https://haversami.firebaseio.com",
    storageBucket: "haversami.appspot.com",
    messagingSenderId: "704224601349"
    });

    //get user details
   // this.currentUser = firebase.auth().currentUser;//to uncomment
    this.userProfile = firebase.database().ref('/users');
    this.semesters = firebase.database().ref('/Semesters/sem');

    // this.id_user=this.af.auth.getAuth().uid; // to uncomment
    this.currentUser='1qfcMAQnglX9jsW5GdLpPko1HqE2';

      this.getUserProfile().on('value', (data) => {
      this.userProfile = data.val();
      this.reg_boolean = this.userProfile.reg_boolean;
      this.department=this.userProfile.depName;
      this.year=this.userProfile.year;

      console.log("enter:"+this.department+this.year);
      console.log(this.reg_boolean);
    });

      this.getSemester().on('value', (data) => {
      this.semesterdetails = data.val();
      this.semester = this.semesterdetails.sem_id;   
      console.log(this.semester);
    });

    //check if user connected
     firebase.auth().onAuthStateChanged((user) => {
       var that=this;
       console.log("#1"+user);
      if (user) {
        if(this.reg_boolean==true){
          console.log("adirush");  
           this.nav.setRoot(TabsPage,{
           dep:this.department,year:this.year,semester:this.semester

        })
          //this.rootPage=TabsPage;
          this.rootPage=TabsPage;
        }
        else{
        this.rootPage = SignupPage;
        }
        console.log("I'm here! HomePage");
      } else {
        this.rootPage = LoginPage;
        console.log("I'm here! LoginPage");
      }
    });

    let loader = this.loadingCtrl.create();
    loader.present();
   this.listenToUserStatusUpdate(loader);
   /*
    let fireBaseUser = firebase.auth().currentUser;
    console.log(fireBaseUser);
   if (fireBaseUser) {
        if(this.reg_boolean=="true"){
          console.log("adirush");  
           this.nav.setRoot(TabsPage,{
           dep:this.department,year:this.year,semester:this.semester

        })
          this.rootPage=TabsPage;
        }
        else{
        this.rootPage = SignupPage;
        }
        console.log("I'm here! HomePage");
      } else {
        this.rootPage = LoginPage;
        console.log("I'm here! LoginPage");
      }

    //this.rootPage = fireBaseUser ? SignupPage : LoginPage;

    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
      Splashscreen.hide();
    });
  }
  listenToUserStatusUpdate(loader: any) {
    firebase.auth().onAuthStateChanged((user) => {
      if(loader)
       loader.dismiss();
      console.log("The User:", user);
      if (user) {
          if(this.reg_boolean=="true"){
          console.log("adirush");  
           this.nav.setRoot(TabsPage,{
           dep:this.department,year:this.year,semester:this.semester

        })
          this.rootPage=TabsPage;
        }
        else{
        this.nav.setRoot(SignupPage);
        }
      } else {
        this.nav.setRoot(LoginPage);
      }
    });
  }


   getUserProfile(): any {
    //return this.userProfile.child(this.currentUser.uid); to uncomment
    return this.userProfile.child(this.currentUser); 
  }
  getSemester():any{
    return this.semesters.child("0");
  }


}
*/

UserPage.ts

import { Component } from '@angular/core';
import { Facebook, NativeStorage } from 'ionic-native';
import { NavController } from 'ionic-angular';
import { UserPage } from '../user/user';

@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})
export class LoginPage {
  FB_APP_ID: number = 1816752771906363;

  constructor(public navCtrl: NavController) {
    Facebook.browserInit(this.FB_APP_ID, "v2.8");
  }

  doFbLogin(){
    let permissions = new Array<string>();
    let nav = this.navCtrl;
    //the permissions your facebook app needs from the user
    permissions = ["public_profile"];

    Facebook.login(permissions)
    .then(function(response){
      let userId = response.authResponse.userID;
      let params = new Array<string>();

      //Getting name and gender properties
      Facebook.api("/me?fields=name,gender", params)
      .then(function(user) {
        user.picture = "https://graph.facebook.com/" + userId + "/picture?type=large";
        //now we have the users info, let's save it in the NativeStorage
        NativeStorage.setItem('user',
        {
          name: user.name,
          gender: user.gender,
          picture: user.picture
        })
        .then(function(){
          nav.push(UserPage);
        }, function (error) {
          console.log(error);
        })
      })
    }, function(error){
      console.log(error);
    });
  }
}

3 个答案:

答案 0 :(得分:3)

我找到了答案

export class LoginPage {
    FB_APP_ID: number = 1816752771906363;

    constructor(public navCtrl: NavController) {
        Facebook.browserInit(this.FB_APP_ID, "v2.8");
    }
}

删除

`FB_APP_ID: number = 1816752771906363;`

`Facebook.browserInit(this.FB_APP_ID, "v2.8");`

它会完美运作。

答案 1 :(得分:-1)

您似乎正在使用Facebook cordova插件,但您没有通过命令添加它。试试这个:

ionic plugin add cordova-plugin-facebook4 

请检查:https://ionicframework.com/docs/v2/native/facebook/

答案 2 :(得分:-1)

添加:

declare const facebookConnectPlugin: any;

declarations.d.ts文件。它对我有用。

参考: Please follow the discussion here