【问题标题】:Retrieving current user data from firebase从 firebase 检索当前用户数据
【发布时间】:2019-05-03 19:26:36
【问题描述】:

我可以检索数据,但它只显示数据库中最后一个用户的数据。用户按其 uid 列出。以下是来自 firebase 的数据的代码和屏幕截图。帮帮我,伙计们!

var database = firebase.database().ref('users');
database.on("child_added", function(snapshot) {
    var snapVal = snapshot.val();
    displayName.innerHTML = snapVal.mUsername;
    console.log(snapVal);

    var badge = document.createElement('span');
    badge.textContent = 'view details';
    badge.setAttribute('class','badge');
    var list = document.createElement('a');
    list.textContent = snapVal.mUsername;
    list.setAttribute('href', '#');
    list.setAttribute('class', 'list-group-item');
    patientList.appendChild(list);
    list.appendChild(badge);
    var userIcon = document.createElement('i');
    userIcon.setAttribute('class','far fa-user');
    list.insertBefore(userIcon, list.childNodes[0])

},
function (errorObject) {
    console.log("The read failed: " + errorObject.code);
});

【问题讨论】:

  • 看看child_added
  • 试试-> database.on("value", function(snapshot) {}

标签: javascript firebase firebase-realtime-database


【解决方案1】:

您应该参考特定用户的节点来检索信息。 您的代码应如下所示:

var userRef = firebase.database().ref(`users/${user_node_key}`); // user_node_key should be the node containing the user info.

您应该能够使用当前用户的对象获取节点密钥。

然后如果您只需要获取一次详细信息,请拨打userRef 上的once,如果您需要听取该用户的所有更改,请拨打on

代码应如下所示:

userRef.once('value').then(function(snapshot) {
  //use snapshot to get the users data
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2017-10-15
    相关资源
    最近更新 更多