komal deep singh chahal komal deep singh chahal - 4 months ago 90
React JSX Question

Firebase is not a constructor error(using firebase with react)

I am using firebase first time and stucked on it for 2 days. looked several example like on stackoverflow, but nothing works.

my firebase version is 3.4.2.

pushData(){

var Firebase = require("firebase");
var ref = new Firebase(" https://differentdatara.firebaseio.com");
var usersRef = ref.child("fish1");
usersRef.set({
alanisawesome: {
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
},
gracehop: {
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
}
});

}


When ever i am running code it gives me "Firebase is not a constructor".

also tried with componentWillMount but nothing works.
I try like this

componentWillMount(){
console.log('yesdoneit');
var Firebase = require("firebase");
this.FirebaseRef = new Firebase(" https://passwordsaves-11b33.firebaseio.com");
}

Answer

As it is written here:

In the new SDKs, you no longer instantiate a database references via new Firebase. Instead, you will initialize the SDK via firebase.initializeApp():

BEFORE

var ref = new Firebase("https://databaseName.firebaseio.com");

AFTER:

var config = {
  apiKey: "apiKey",
  authDomain: "projectId.firebaseapp.com",
  databaseURL: "https://databaseName.firebaseio.com"
};

firebase.initializeApp(config);

var rootRef = firebase.database().ref();

Since, your firebase version is 3.4.2, you have to do it like in after section.