FIREBASE + NODEJS error: Firebase App named '[DEFAULT]' already exists

Asked

Viewed 887 times

2

I have a CRUD made with NODEJS + EXPRESS, using FIREBASE.

I add a record normally, but when I add the next one, it gives me the error:

[DEFAULT]: Firebase: Firebase App named '[DEFAULT]' already exists (app/Duplicate-app).

If I restart the server, it works. From what I’ve seen, I’m triggering the connection multiple times, but I don’t know where to fix it.

Can you explain to me?

var fb = this._firebase.database();

var result = fb.ref().child('clientes/').orderByChild('empresa').equalTo(cliente.cnpj);
result.once('value').then(function(snapshot){

    var erros;
    if(snapshot.length < 0){
        erros = { 
            erro: 'CNPJ já cadastrado.'
        };
        return erros;
    }

    var key = fb.ref('clientes/').push(cliente).key;

    var mensagem = {
        msg: 'Cliente cadastrado com sucesso!',
        status: 'success'
    }

    res.render('clientes/clientes', { validacao: {}, dadosForm: {}, mensagem: mensagem });
});

3 answers

2


This error usually occurs when the function

firebase.initializeApp(firebaseConfig)

is called more than once. Check where it is occurring is called, if it is not in a loop or if it is being called whenever a new request is made.

0

This error occurs when Voce calls firebase.initializeApp(firebaseConfig) more than once, so the ideal is to create a name for each app.

This is the start app function that gets two parameters:

function initializeApp(options: Object, name?: string): firebase.app.App;

Pass the name that should be a string in the second parameter :

const options : Object = {
    apiKey: "apiKey",
    authDomain: "projectId.firebaseapp.com",
    databaseURL: "https://databaseName.firebaseio.com",
    storageBucket: "bucket.appspot.com"
  };

const app = firebase.initializeApp(options, "name");

0

This error occurs when Voce calls firebase.initializeApp(firebaseConfig) more than once.

To fix this just do the following:

if(!firebase.apps.length){
   firebase.initializeApp(firebaseConfig)
}

This way it will check if there is an app already initialized and if there is no if.

NOTE: You can modify the if to firebase.apps.length < 1 or something similar to this, I used denial because the JS recognizes 0 as false

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.