Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

324
Views
Client is Offline error while using firebase realtime database

I am trying to make a small database system for my client that will capture his website's registered users name and there spent amount. Based on there spent amount he will get some gifts. Using firebase realtime database i am able to store user spent amount but when i try to get data from database. I am getting the following Error from the line: get(child(userDbRef, "User-ID-"+userID)):

"Uncaught (in promise) Error: Error: Client is offline."

initailData(); will work if user does not have data stored already and if the data has been stored once then runInitialFunction(); will work and get/retrieve data from database.

Here is my code

  // Import the functions you need from the SDKs you need
  import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-app.js";
  // TODO: Add SDKs for Firebase products that you want to use
  // https://firebase.google.com/docs/web/setup#available-libraries

  // Your web app's Firebase configuration
  const firebaseConfig = {
    apiKey: "AIzaSyAfA1HIkamXEFtDidPkpxaltDVsWpFWepE",
    authDomain: "thundersmmpanel-77fb2.firebaseapp.com",
    databaseURL: "https://thundersmmpanel-77fb2-default-rtdb.firebaseio.com",
    projectId: "thundersmmpanel-77fb2",
    storageBucket: "thundersmmpanel-77fb2.appspot.com",
    messagingSenderId: "867306490186",
    appId: "1:867306490186:web:93407cb6d8b0e9abe9d9b3"
  };

  // Initialize Firebase
  const app = initializeApp(firebaseConfig);

  
  //<!--my code-->
  import {getDatabase, ref, set, get, child, update}
  from "https://www.gstatic.com/firebasejs/9.6.0/firebase-database.js";
  
  const userID = {{user['id']}};
  const userName = "{{user['username']}}";
  const pointsDb = getDatabase();
  
  //<!-- initial data -->
  function initailData() {
    set(ref(pointsDb, "User-ID-"+userID),{
        UserName: userName,
        InitialSpent: {{user['spent']}},
        ThisRun: 1
    })
    .then(function(){
        alert("Done");
    })
    .catch(function(error){
        alert("Error "+error);
    });
    console.log("initailData");
  }
  
  
  function runInitialFunction() {
    const userDbRef = ref(pointsDb)
    get(child(userDbRef, "User-ID-"+userID)).then((snapshot)=>{
        if(snapshot.exists()){
            
        }else{
            initailData();
        }
    });
    console.log("runInitialFunction");
  }
  runInitialFunction();
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!