从Firebase实时数据库中检索数据并自动填充到文本框中

时间:2019-01-22 05:03:34

标签: javascript firebase firebase-realtime-database

我想从firebase实时数据库中检索数据,并用用户注册后已经在其中键入的数据库内部数据自动填充。我将用户数据保存在他们的UID下,现在我遇到了一个问题,无法通过用户UID从数据库中检索数据,并通过当前登录用户uid自动为用户填充数据。

我的Firebase数据库 https://imgur.com/a/BlzFADs

要自动填充的地方 https://imgur.com/a/urvNAFe

我为我的代码尝试了所有可能的解决方案,但我似乎无法使其正常工作,我在stackoverflow上找到的所有解决方案,但仍然对我不起作用。

我的JS文件

 firebase.database().ref('Users/User Information').child(uid).once('value', 
 function(snapshot) {

   document.getElementById('name').value = snapshot.child("Name").val();
 });

我的HTML文件

 <td>Name:</td>
 <td>
     <input type="text" id="name" name="name" style="font-size:10pt; width:200px;" placeholder="Enter Name Here" required>
 </td>  

现在,我想根据其UID自动为当前登录用户填充数据。

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

firebase.database().ref('Users/User Information/'+uid.value).once('value', 
 function(snapshot) {
   document.getElementById('name').value = snapshot.val().Name;
});

或者使用箭头功能:

firebase.database().ref('Users/User Information/'+uid.value).once('value', (snapshot) => 
{
  document.getElementById('name').value = snapshot.val().Name;
});

答案 1 :(得分:0)

我通过使用document.getelementbyID完成此操作,并将其链接到数据库的子级。