Попробуйте разными способами подключить данные из Firebase к веб-сайту, но безуспешно.
Я пытался подключить данные из Firebase к html Web. все в порядке, за исключением того, что данные из Firebase никогда не обновляются и просто отображают значение N/A. Что-то не так?
const firebaseConfig = {
apiKey: "api",
authDomain: "xx.firebaseapp.com",
databaseURL: "https://xxx.firebasedatabase.app",
projectId: "xx",
storageBucket: "xx.firebasestorage.app",
messagingSenderId: "11",
appId: "11",
measurementId: "11"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const database = firebase.database();
// UI Update Function
function updateUI(elementId, value, unit = '', timestampId = null) {
document.getElementById(elementId).innerHTML = value + unit;
if (timestampId) {
const currentTime = new Date().toLocaleTimeString();
document.getElementById(timestampId).innerHTML = `Last updated: ${currentTime}`;
}
}
// Realtime updates
const NO2Ref = database.ref('sensor/NO2');
const NO2FreshnessElement = document.getElementById('NO2-freshness');
NO2FreshnessElement.innerHTML = `Data age: ${NO2Age} seconds`;
function updateUI(elementId, value, unit = '', timestampId = null, freshnessId = null) {
document.getElementById(elementId).innerHTML = value + unit;
if (timestampId) {
const currentTime = new Date().toLocaleTimeString();
document.getElementById(timestampId).innerHTML = `Last updated: ${currentTime}`;}
stateRef.on('value', function(snapshot) {
const state = snapshot.val();
if (state) {
CORef.on('value', function(snapshot) {
const NO2 = snapshot.val();
const currentTime = new Date().toLocaleTimeString();
updateUI('NO2', NO2, 'ppm', 'NO2-time', 'NO2-freshness');}
else {
// If state is invalid, display N/A for all parameters
document.getElementById('NO2').textContent = 'N/A';
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... ase-to-web