Integrate Firebase into ionic v1

1

Hi, I have problems integrating Firebase in my ionic v1 app.

The way I am integrating it is as follows:

I create a config.js:

angular.module('starter.configs', [])

.constant("CONFIG", {
  "FIREBASE_API": 'apiiiii******+',
  "FIREBASE_AUTH_DOMAIN": 'caif-**a.firebaseapp.com',
  "FIREBASE_DB_URL": 'https://caif-a***a.firebaseio.com',
  "FIREBASE_STORAGE": 'caif-a**.appspot.com',
  "MESSAGE_SENDER_ID": '19128****'
});

importo firebase.js ,angularfire.js y config.js en el index.hrml:

<script src="lib/ionic/js/angular/angular-resource.js"></script>
<script src="lib/firebase/firebase.js"></script>
<script src="lib/angularfire/dist/angularfire.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/services.js"></script>
<script src="js/config.js"></script>

then in app.js I integrate firebase and starter.configs .

angular.module('starter', ['ionic','starter.controllers','ngResource','firebase','starter.configs','starter.services'])

and finally the beginning of the following way in the same app.js

  .run(function($ionicPlatform,CONFIG) {
     $ionicPlatform.ready(function() {

  if(window.cordova && window.cordova.plugins.Keyboard) {

    cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    cordova.plugins.Keyboard.disableScroll(true);


  }
  if(window.StatusBar) {
    StatusBar.styleDefault();
  }

  firebase.initializeApp({
    apiKey: CONFIG.FIREBASE_API,
    authDomain: CONFIG.FIREBASE_AUTH_DOMAIN,
    databaseURL: CONFIG.FIREBASE_DB_URL,
    storageBucket: CONFIG.FIREBASE_STORAGE,
    messagingSenderId: CONFIG.FIREBASE_STORAGE
  });


});
})

to use for example $firebaseAuth in services I think the following:

.factory("Auth", ["$firebaseAuth",function($firebaseAuth) {
return $firebaseAuth();
}
])

and from the controller it would be something like this:

.controller('InicioCtrl', function($scope, $stateParams, $location, Auth) {

  $scope.LoginUser = function(email, pass) {

    var email = email;
    var password = pass;

firebase.auth().signInWithEmailAndPassword(email, password).then(function(firebaseUser) {
  $location.path('tab/ReservaHora');
}).catch(function(error) {
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

};

});

The problem is that from time to time on the local ionic server the following error occurs:

  

FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp () (app / no-app).       at Z ( link )       at a ( link )       at Object.c [as auth] ( link )       at link       at Object. ( link )       at Object.invoke ( link )       at Object.enforcedReturnValue [as $ get] ( link )       at Object.invoke ( link )       at link       at getService ( link )

and the application does not work, it remains as it is seen in the image. This since ionic serve when refreshing is solved. But when doing a ionic build ios compile and run on the iphone the application does not work or just in case, it does not enter when registering and nothing.

    
asked by Hernan Humaña 23.02.2017 в 16:31
source

1 answer

1

I think the problem is that the initialization should not go in your app.js, but in the factory where you will create the reference. Try the following changes:

in app.js delete firebase.initializeApp sentence and send CONFIG of parameter

  .run(function($ionicPlatform) {
     $ionicPlatform.ready(function(CONFIG) {

and in the factory you can already initialize the reference to firebase:

.factory("Auth", ["$firebaseAuth",function($firebaseAuth) {
  firebase.initializeApp(CONFIG);
  var ref = firebase.database().ref();
  var auth = $firebaseAuth();
}
])
    
answered by 24.02.2017 / 15:03
source