Estoy tratando de crear un pequeño sistema de base de datos para mi cliente que capturará el nombre de los usuarios registrados de su sitio web y la cantidad gastada. Según la cantidad gastada, recibirá algunos obsequios. Al usar la base de datos en tiempo real de Firebase, puedo almacenar la cantidad gastada por el usuario, pero cuando intento obtener datos de la base de datos. Recibo el siguiente error de la línea: get(child(userDbRef, "User-ID-"+userID)) :
"Uncaught (in promise) Error: Error: Client is offline."
initailData(); funcionará si el usuario aún no tiene datos almacenados y si los datos se han almacenado una vez, entonces runInitialFunction(); funcionará y obtendrá/recuperará datos de la base de datos.
Aquí está mi código
// 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();