我遇到了 firebase 的问题,它在我的网站上显示值,但仅当我手动输入它们时才显示。但是当我的 esp8266 将数据发布到 firebase 时,我的网站无法读取数据。它只是说空。值和类型与 esp 相同,但仅在我创建它时显示。 esp 中的值会被再次写入,因此会出现 2 个同名的值。出于安全原因,firebaseConfig 为空。
感谢您提前提供的任何帮助!
这是我的JavaScript代码:
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-app.js";
import { getDatabase, ref, onValue } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-database.js";
// Initialize Firebase with your configuration
const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
const app = initializeApp(firebaseConfig);
// Reference to your data in the database
// Reference to your data in the database for Humidity 3x1 and Humidity 4x1
var humidity3x1Ref = ref(getDatabase(), "Humidity3x1");
var humidity4x1Ref = ref(getDatabase(), "Humidity4x1");
// Get the data and update the HTML for Humidity 3x1
onValue(humidity3x1Ref, function(snapshot) {
var dataValue = snapshot.val();
var pathElement = document.querySelector(".circular-chart.orange .circle");
var textElement = document.querySelector(".circular-chart.orange .percentage");
// Update the circular chart and percentage text
updateCircularChart(pathElement, textElement, dataValue);
});
// Get the data and update the HTML for Humidity 4x1
onValue(humidity4x1Ref, function(snapshot) {
var dataValue = snapshot.val();
var pathElement = document.querySelector(".circular-chart.green .circle");
var textElement = document.querySelector(".circular-chart.green .percentage");
// Update the circular chart and percentage text
updateCircularChart(pathElement, textElement, dataValue);
});
// Function to update circular chart and percentage text
function updateCircularChart(pathElement, textElement, dataValue) {
pathElement.setAttribute("stroke-dasharray", `${dataValue}, 100`);
textElement.textContent = `${dataValue}%`;
}
确保您在 Firebase 管理门户上正确配置了安全规则 - https://console.firebase.google.com。
从您的描述来看,您似乎只允许访问特定用户拥有/创建的数据。