0

I'm trying to save contact data from an HTML5 form to firebase but I keep getting the error firebase.database is not a function in my console.

// Your web app's Firebase configuration
var firebaseConfig = {
    apiKey: "***********************",
    authDomain: "*********************",
    databaseURL: "*********************",
    projectId: "*********************",
    storageBucket: "",
    messagingSenderId: "***************",
    appId: "**********************"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);

//Reference Messages Collection
var messagesRef = firebase.database().ref('messages');

/* listen for form submit */
document.getElementById('contactForm').addEventListener('submit', submitForm);

//submitForm function
function submitForm(e){
    e.preventDefault();

  //get values
  const name = getInputVal('name');
  const email = getInputVal('email');
  const phone = getInputVal('tel');
  const message = getInputVal('message');

  saveMessage(name, email, phone, message);


}

// Function to get form values
function getInputVal(id){
    return document.getElementById(id).value;
}

// Save the message to Firebase
function saveMessage(name, email, phone, message){
    const newMessageRef = messagesRef.push();
    newMessageRef.set({
        name: name,
        email: email,
        phone: phone,
        message: message
    });
}

I don't expect any errors at this point and I have followed different tutorial videos but I am getting the same error while its working in the video's I'm watching

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    Possible duplicate of [firebase.database is not a function](https://stackoverflow.com/questions/38248723/firebase-database-is-not-a-function) – Reegan Miranda Aug 14 '19 at 11:30

1 Answers1

0

I was facing the same error a while ago and I found the answer it was quite different from what I expected.

Convert

<script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-app.js"></script>

to

<script src="https://www.gstatic.com/firebasejs/7.2.3/firebase.js"></script>

Remove -app it works. I do not know the reason.It worked for me.I hope it works for u.

Abhinav Mani
  • 300
  • 1
  • 2
  • 11