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();