如何从uid存储的firebase中检索用户数据

时间:2020-08-01 09:40:18

标签: javascript html firebase-realtime-database

我正在尝试使用Html和js从firebase数据库中检索数据,我对Web开发人员不是很好,所以这是我的第一个Web开发人员,对此不太熟悉,因此我的用户由用户uid {{3}存储}

她是我的密码

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Side Navigation Bar</title>
    <link rel="stylesheet" href="sidebarStyle.css">
    <script src="https://kit.fontawesome.com/b99e675b6e.js"></script>
</head>
<body>
<div class="wrapper">
            <table>
                <tr>
                    <th>name:</th>
                    <th>phone:</th>
                    <th>id:</th>
                </tr>
                <tbody id="table">

                </tbody>
            </table>
        </div>
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-app.js"></script>

<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-storage.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-database.js"></script>


<script>
  // Your web app's Firebase configuration
  var firebaseConfig = {
    apiKey: "AIza........",
    authDomain: "........",
    databaseURL: ".........",
    projectId: ".......",
    storageBucket: "........",
    messagingSenderId: ".......",
    appId: "1:.....:web:.......",
    measurementId: "G-......"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
</script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"  
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="   
crossorigin="anonymous"></script>

<script src="usersTable.js"></script>
        
 </body>
</html>

我的Javascript代码在一个单独的文件中-> usersTable.js另一个问题是,当我编写Firebase时,没有自动完成出现,我确实安装了打字稿,我尝试的代码遵循了这家伙的指导,但没有这样做。为我工作here's the data struct 如果我的代码有任何问题,或者我必须安装firebase工具,或者不让我知道

1 个答案:

答案 0 :(得分:0)

要检索已登录用户的数据,请执行以下操作:

[假设您需要登录用户的详细信息]

// get current logged in user details
const user = firebase.auth().currentUser;

// get the branch of the user
const userBranch = firebase.database().ref('/users/'+user.uid);

// retrieve the data from that branch - Remember this is an asynchronous function - it returns promise
userBranch.once('value').then((snapShot)=>{
   var userPhoneNo = snapShot.val().phone;
   var userPic = snapShot.val().profileImageUrl;
   var userName = snapShot.val().username;
});

资源:Link

如果您想了解有关异步函数的更多信息-Link

关于第二个问题,JavaScript for Firebase中没有可用于自动完成的扩展。

编辑1:

[从用户分支获取所有用户]

// this will get the whole user branch
const userBranch = firebase.database().ref('/users');

// retrieve the data from the database
userBranch.once('value').then((snapShot)=>{

 var userDetaills = snapShot.val()

 userDetails.forEach((user)=>{
  let userPhone = user.phone;
  let userPic = user.profileImageUrl;
  let userName = user.username;

  // your rest of the logic
 });
});