我的目標是能夠通過javascript從firebase上的實時資料庫中檢索某些值。我已經配置了所有東西并且它運行良好,但是當我單擊一個簡單的按鈕時,我不知道如何檢索各個欄位。
這是我希望能夠采用的值的一個示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button type="submit" id="searchData" >Search data</button>
</body>
</html>
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.8/firebase-app.js";
import { getDatabase, set, ref ,push, child, onValue} from "https://www.gstatic.com/firebasejs/9.6.8/firebase-database.js";
const firebaseConfig = {
apiKey: "********************************",
authDomain: "********************************",
databaseURL: "********************************",
projectId: "********************************",
storageBucket: "****************************",
messagingSenderId: "******************",
appId: "********************************"
};
const app = initializeApp(firebaseConfig);
const db = getDatabase(app);
searchData.addEventListener('click',(e) => {
//
});
</script>
uj5u.com熱心網友回復:
您可以嘗試使用我始終實作的這個示例,注意我的主檔案是用戶,然后我的鍵值作為 id 是 userId 然后我有屬性用戶名和用戶郵件。試試這個:
I want to obtain a name:
const dbRef=ref(db, 'users/' userId);
onValue(dbRef, (snapshot)=>{
const data=snapshot;
for(let i in data){
console.log(snapshot.val().username);
txtCont.innerHTML=`<p>${snapshot.val().username}</p>`;
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471749.html
標籤:javascript 火力基地
上一篇:Symfony6:使用PHP(bootstrap.php)設定/更新/操作環境變數(APP_ENV)
下一篇:嘗試通過sendEmailVerification();使用reactfirebasehook創建用戶;添加但沒有驗證它創建用戶:這是下面的代碼:
