从Firebase检索当前用户数据

时间:2018-12-02 06:09:35

标签: javascript firebase firebase-realtime-database

我能够检索数据,但是它仅显示数据库中最后一个用户的数据。用户按其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);
});

Database, users is the child of the root.

1 个答案:

答案 0 :(得分:1)

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

public void setUpPanels() {
    setUpTextInputField();
    setUpGenerateButton();
    setUpPropsPanel();
    setUpButtons();
    setUpClearButton();


    leftPanel = new JPanel();
    rightPanel = new JPanel();
    leftPanel.setLayout(new GridBagLayout());
    rightPanel.setLayout(new GridBagLayout());

    leftPanel.setBackground(Color.gray);
    rightPanel.setBackground(Color.white);

    /*
     * create a new constraints for each new component, to avoid bugs
     */
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 0;
    leftPanel.add(inputL, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 1;
    leftPanel.add(inputField, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.insets = new Insets(0, 15, 0, 0);
    leftPanel.add(generateButton, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 2;
    leftPanel.add(propsL, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 3;
    constraints.insets = new Insets(10, 0, 0, 0);
    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.WEST;
    leftPanel.add(propsPanel, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.ipadx = 30;
    constraints.ipady = 70;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = new Insets(10, 15, 0, 0);
    leftPanel.add(buttonPanel, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 4;
    constraints.insets = new Insets(10, 0, 0, 0);
    constraints.anchor = GridBagConstraints.WEST;
    leftPanel.add(clearButton, constraints);

    add(leftPanel);
    add(rightPanel);
}

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

如果只需要一次获取详细信息,然后致电var userRef = firebase.database().ref(`users/${user_node_key}`); // user_node_key should be the node containing the user info. 上的once,或者如果您需要收听该用户的所有更改,则致电userRef

代码应类似于:

on