10

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)
4
  • Can you please also mention the firebase SDK version you are using? Is it v2.x or v3.x?
    – praneybehl
    Jun 1, 2016 at 1:31
  • Looking at it again, in the "xxxxxyyyyyzzzzz.firebaseio.com" are using your database name or your project id? with v3.x I think you need to use your project id instead. For instance you can find your project id in the new Firebase console. When you navigate to the Database in the firebase console check the url "console.firebase.google.com/project/<project-id>/database/…". Hope it helps.
    – praneybehl
    Jun 1, 2016 at 2:07
  • Hi, thanks for your help, the fact is that I am using the version 3.x and use the id of the project (project_id). I have placed the message displayed after pressing the F5 key Jun 1, 2016 at 5:25
  • 1
    I've found the problem is that the code that throws the SDK for connection, put the name to the variable initialization "databaseUrl" but actually should be "databaseURL". Thanks!!! Jun 1, 2016 at 5:47

3 Answers 3

11

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

if (!firebase.apps.length) {
    firebase.initializeApp({});
}
0
6

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/");
1
  • even applied this method, still have the same error @lommaj Oct 11, 2017 at 2:29
0

You should execute firebase.initializeApp only ONCE per app, outside your router.get By the way, your exports look weird/wrong ... if it is a module, why are you calling router.get directly? As soon as you require your module, It would get executed. Why are you exporting router?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.