skip to Main Content

I am using firebase database in nodejs.

I simply want to fetch all the users that exists in my firebase database.

Currently I am trying this but it is only return one single user:

var config = {
    apiKey: "AIzaSyBmgbfgdhs4efg9gFo",
    authDomain: "my-test-app.firebaseapp.com",
    databaseURL: "https://my-test-app-default-rtdb.firebaseio.com",
    projectId: "my-test-app",
    storageBucket: "my-die-app.appspot.com",
    messagingSenderId: "534345345",
    appId: "1:534345345:web:cceb16e6vvdd456",
    measurementId: "G-YDZ8Y87GETR",
};

firebase.initializeApp(config);

var ref = firebase.database().ref("users");

ref.once("value", function (snapshot) {

    snapshot.forEach((child) => {

        console.log(child)
    });
});

In firebase users I can only see this single user aswell, but infact I have more users there:

{
  "XG0hYDjhekjdfJX5zLmfUFPQBifkmZA3": [
    "XG0hYDjhekjdfJX5zLmfUFPQBifkmZA3",
    "[email protected]",
    "835457568",
    "Michael James ",
    "[email protected]"
  ]
}

And this is the only user I am getting in code aswell.

I am not the owner of this database, may be thats the reason?

Is there any specific way to retrieve all users list?

2

Answers


  1. I believe the question answers itself

    In firebase users I can only see this single user

    If there is only only user node in the /users node as shown in your question

    users {
      "XG0hYDjhekjdfJX5zLmfUFPQBifkmZA3": [
        "XG0hYDjhekjdfJX5zLmfUFPQBifkmZA3",
        "[email protected]",
        "835457568",
        "Michael James ",
        "[email protected]"
      ]
    }
    

    that means only one node exists – therefore only one node can be retrieved.

    The confusion may be that while Firebase has users that can authenticate, those users are stored in the Firebase back-end server only. It doesn’t necessarily mean other user data was written to the /users node.

    In other words, if the app code does not specifically write data to /users, it won’t exist.

    Check your authentication code and see if it writes data to /users at some point – if not, there’s your problem.

    If that’s the issue, which I suspect it is, you can’t retrieve a list of Firebase users from the SDK directly, but you can using Firebase Admin coupled with Cloud Functions.

    Here’s an example: Get All Users

    Alternatively (and my suggested path), you can also add code to the app so when users authenticate, if their users node doesn’t exist within /users, create it (using the users uid returned from the Auth parameter as the node key)

    Login or Signup to reply.
  2. What user are you logged in as? Is that the user you can see? What are your Firebase security rules like?

    If you have reasonably secure rules setup, that would be the reason users can only see their own nodes.

    See these docs: https://firebase.google.com/docs/database/security#section-authorization

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search