首页 > 解决方案 > 可以使用 onAuthStateChanged 中的 user_id 从 firebase 读取数据

问题描述

我只是制作简单的输入代码,它适用于userId. 但是,如果我使用.child(userId). 我尝试在穿上之前将其转换为字符串child(),但仍然无法正常工作。如果我userId像这样使用它,它会起作用.child('iOtzRxxxxxxxxxxxxxxx')。如果我使用.child(userId),它会给我这样的错误:错误:Reference.child 失败:第一个参数是无效路径 =“未定义”。路径必须是非空字符串,并且不能包含“.”、“#”、“$”、“[”或“]”。

.child(userId)如果我可以输入我的数据,我如何读取我的数据.child(userId)

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Signin Template for Bootstrap</title>
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <script src="https://www.gstatic.com/firebasejs/4.11.0/firebase.js"></script>
        <script src="core.js"></script>
    </head>
    <body>
        <div class="container">
            <input type="text" id="masukan">
            <button class="btn btn-primary small" id="input">Input Data</button>
            <button class="btn btn-danger small" id="keluar">Signout</button>
            <button class="btn btn-success small" id="fresh_uid">Fres UID</button>
            <h1 id="userss"></h1>
            <h1 id="user_uid"></h1>
            <pre id="json"></pre>
            
        </div>
        <script>
            
            var txtinput = document.getElementById('masukan');

            var inputRed    = firebase.database().ref('users');
            
            var user_id; //User uid from onAuthStateChanged
            
            //Input value
            document.getElementById('input').addEventListener('click', e =>{
                inputRed.child(user_id).push({username: txtinput.value});
            });
            
            //Read data
            //inputRed.child('USER_UID_STRING').on('child_added', function(snapshot){
            
            inputRed.child(user_id).on('child_added', function(snapshot){
               document.getElementById('userss').innerText = snapshot.val().username;
            });
            
            //Read UID
            document.getElementById('fresh_uid').addEventListener('click', function(e){
                document.getElementById('user_uid').innerText = user_id;
            });
            
            //Sign Out
            document.getElementById('keluar').addEventListener('click', e =>{
                firebase.auth().signOut();
            });
            
            //Read current user
            firebase.auth().onAuthStateChanged(firebaseUser =>{
                if(firebaseUser){
                    var namaLengkap     = firebaseUser.displayName;
                    //document.getElementById('json').textContent = JSON.stringify(firebaseUser, null, ' ');
                    user_id = firebaseUser.uid;
                }
            });
        
        </script>
        <script src="js/jquery-3.3.1.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
    </body>
</html>

标签: javascriptfirebasefirebase-realtime-databasefirebase-authentication

解决方案


您的问题非常令人困惑,我根本没有得到它。但看看你的代码 - 你设置user_id

firebase.auth().onAuthStateChanged(firebaseUser =>{
    if(firebaseUser){
        var namaLengkap     = firebaseUser.displayName;
        //document.getElementById('json').textContent = JSON.stringify(firebaseUser, null, ' ');
        user_id = firebaseUser.uid;
    }
});

这是您在脚本中做的最后一件事。而且是有条件的。但是你user_id以前用过很多次。所以每次你使用user_id,例如:

document.getElementById('input').addEventListener('click', e =>{
    inputRed.child(user_id).push({username: txtinput.value});
});

user_id评估为undefined。它没有价值。


推荐阅读