简体   繁体   中英

Retrieve all registered users in firebase

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",
    "user1@gmail.com",
    "835457568",
    "Michael James ",
    "user1@gmail.com"
  ]
}

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?

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",
    "user1@gmail.com",
    "835457568",
    "Michael James ",
    "user1@gmail.com"
  ]
}

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)

You should call the val() method of the child DataSnapshot as follows:

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

    snapshot.forEach((child) => {

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

See also the exmaples in the doc .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM