可以使用onAuthStateChanged中的user_id从firebase读取数据

问题描述 投票:-1回答:1

我只是制作简单的输入代码,它适用于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>
javascript firebase firebase-realtime-database firebase-authentication
1个回答
0
投票

你的问题非常混乱,我根本没有得到它。但是看看你的代码 - 你设置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。它没有任何价值。

© www.soinside.com 2019 - 2024. All rights reserved.