Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase App named '[DEFAULT]' already exists & google firebase reference does not work on the server

I followed all the steps for the server configuration in https://firebase.google.com/docs/database/server/retrieve-data#section-start, but when I try to create the reference in the browser tells me that the application called [default] already exists. At that time if preiono F5 then tells me that there is "databaseUrl" I must make sure to provide that data when starting the application firebase.

var express = require('express');
var router = express.Router();

var firebase = require('firebase');

/* GET home page. */
router.get('/', function(req, res, next) {

  firebase.initializeApp({
    serviceAccount: "aaaabbbbcccc.json",
    databaseUrl: "https://xxxxxyyyyyzzzzz.firebaseio.com/" // <<<<--- it can not find
  });

  var db = firebase.database();   // <<<<---- Here is a problem
  var ref = db.ref('vistas/principal');
  ref.once('value', function(data){

    res.render('index', { title: 'Express' });

  });


});

module.exports = router;

I created other authorizations, other keys, etc. But nothing works. Thank you.

Node -v = v4.2.6 Firebase v3.x

FIREBASE FATAL ERROR: Can't determine Firebase Database URL. Be sure to include databaseURL option when calling firebase.intializeApp().

Error: FIREBASE FATAL ERROR: Can't determine Firebase Database URL.  Be sure to include databaseURL option when calling firebase.intializeApp(). 
    at Error (native)
    at ad (c:\TsPrj\_pruebas\vistas-realtime-desde-firebase\node_modules\firebase\database-node.js:37:278)
    at Object.firebase.INTERNAL.registerService.Reference [as database] (c:\TsPrj\_pruebas\vistas-realtime-desde-firebase\node_modules\firebase\database-node.js:238:113)
    at O.u (c:\TsPrj\_pruebas\vistas-realtime-desde-firebase\node_modules\firebase\app-node.js:16:94)
    at Object.d [as database] (c:\TsPrj\_pruebas\vistas-realtime-desde-firebase\node_modules\firebase\app-node.js:18:182)
    at c:\TsPrj\_pruebas\vistas-realtime-desde-firebase\routes\index.js:14:21
    at Layer.handle [as handle_request] (c:\TsPrj\_pruebas\vistas-realtime-desde-firebase\node_modules\express\lib\router\layer.js:95:5)
    at next (c:\TsPrj\_pruebas\vistas-realtime-desde-firebase\node_modules\express\lib\router\route.js:131:13)
    at Route.dispatch (c:\TsPrj\_pruebas\vistas-realtime-desde-firebase\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (c:\TsPrj\_pruebas\vistas-realtime-desde-firebase\node_modules\express\lib\router\layer.js:95:5)
like image 446
Yerson Crespo Avatar asked Jun 01 '16 00:06

Yerson Crespo


2 Answers

You can check if app is already loaded since it should be loaded once. One way is:

if (!firebase.apps.length) {
    firebase.initializeApp({});
}
like image 172
Reza Baradaran Gazorisangi Avatar answered Nov 14 '22 04:11

Reza Baradaran Gazorisangi


here is an answer I wrote for this question

This is an issue I ran into as well upgrading to the new version of Firebase. You might want two separate firebase apps initialized, but I just wanted to use the refs in two different locations in my app and I was getting the same error.

What you need to do for this situation is to create a firebase module for your app that only initializes firebase once, then you import or require it elsewhere in your app.

Your should copy the entire config object from the firebase console and paste that in here with the API key and everything.

Here is an example of the way I made my module: modules/firebase.js

import firebase from 'firebase';
var firebaseConfig = {
  apiKey: "some-api-key",
  authDomain: "some-app.firebaseapp.com",
  databaseURL: "https://some-app.firebaseio.com",
  storageBucket: "some-app.appspot.com",
};

var FbApp = firebase.initializeApp(firebaseConfig);
module.exports = FbApp.database(); //this doesnt have to be database only

And then elsewhere in your application you simply:

import Firebase from '/your/module/location'
var messagesRef = Firebase.ref("messages/");

Or

var Firebase = require('/your/module/location');
var messagesRef = Firebase.ref("messages/");
like image 29
lommaj Avatar answered Nov 14 '22 05:11

lommaj