1

Here is my error that is showing after click on accounts Button plz help me into this i am new to programing any help will be appreciated plz help me into this i had also attached image of firebase database the database is aslso set to open for write and read to public:

 Read.js:2 Uncaught TypeError: firebase.database(...).ref(...).then is not a function
at getdata (VM1188 Read.js:2)
at HTMLButtonElement.onclick ((index):8)
 getdata @ Read.js:2
 onclick @ (index):8

function getdata() {

   const db = firebase.database().ref('test1-8c715-default-rtdb');
    db.once("value").then(function(snapshot) {


    var Name=snapshot.val().Name;
    var Email=snapshot.val().Email;
    var contactno=snapshot.val().phoneno;

   

    document.getElementById("Name").innerHTML=Name;
    document.getElementById("Email").innerHTML=Email;
    document.getElementById("phoneno").innerHTML=phoneno;
})

}

<html>
<head>
    <title>Accounts</title>
    <script type="text/javascript" src="Read.js"></script>
</head>
<body>
<center>
    <button type="button"  onclick="getdata()">Accounts</button>
    <!-- <button type="button" onclick="getdata();">Account</button> -->
</center>
<center>
    <p>Email: <strong id="Email"></strong></p>
    <p>Contactno: <strong id="phoneno"></strong></p>
    <p>Name: <strong id="Name"></strong></p>
</center>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.4.2/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/8.4.2/firebase-analytics.js"></script>
<script>
  // Your web app's Firebase configuration
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
  var firebaseConfig = {
    apiKey: ".............",
    authDomain: "test1-8c715.firebaseapp.com",
    databaseURL: "https://test1-8c715-default-rtdb.firebaseio.com",
    projectId: "test1-8c715",
    storageBucket: "test1-8c715.appspot.com",
    messagingSenderId: "401523276961",
    appId: "1:401523276961:web:fc53ded09831fbc7690...4e0",
    measurementId: "G-PS.....5KQT2"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
</script>
  <script type="text/javascript" src="Read.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.4.2/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.4.2/firebase-database.js"></script>
</body>
</html>
Mian Rehan
  • 11
  • 2
  • Never post personal info or critical info like api-keys, replace with dots or so. – Grumpy Apr 29 '21 at 12:34
  • @Grumpy Those keys are not a secret in the case of Firebase. See https://stackoverflow.com/questions/37482366/is-it-safe-to-expose-firebase-apikey-to-the-public/37484053#37484053 – Frank van Puffelen Apr 29 '21 at 13:38

1 Answers1

0

The error comes from this line:

const db = firebase.database().ref('test1-8c715-default-rtdb').then(function (snapshot) {

The first part should be:

const db = firebase.database('your-full-database-url').ref();

In addition this is a synchronous operation, so it doesn't return a promise that you call then() on. Instead this merely sets up a reference to the database.

To then read data from the database, use once()orget()`:

db.once("value").then(function(snapshot) {
  ...

I think you'll need to include the full URL in that ref('test1-8c715-default-rtdb'), so be sure to copy it from the database panel in the Firebase console of your project. And finally, you'll need to pass that URL to the database call and not to ref().

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807