简体   繁体   English

检索firebase中的所有注册用户

[英]Retrieve all registered users in firebase

I am using firebase database in nodejs.我在 nodejs 中使用 firebase 数据库。

I simply want to fetch all the users that exists in my firebase database.我只是想获取我的 firebase 数据库中存在的所有用户。

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:在 firebase 用户中我也只能看到这个单个用户,但实际上我在那里有更多用户:

{
  "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在firebase个用户中我只能看到这个单个用户

If there is only only user node in the /users node as shown in your question如果/users节点中只有用户节点,如您的问题所示

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.令人困惑的是,虽然 Firebase 有可以进行身份验证的users ,但这些用户仅存储在 Firebase 后端服务器中。 It doesn't necessarily mean other user data was written to the /users node.这并不一定意味着其他用户数据已写入/users节点。

In other words, if the app code does not specifically write data to /users , it won't exist.换句话说,如果应用程序代码没有专门将数据写入/users ,它就不会存在。

Check your authentication code and see if it writes data to /users at some point - if not, there's your problem.检查您的身份验证代码,看看它是否在某个时候将数据写入/users - 如果没有,那就是您的问题。

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 .如果这是问题所在,我怀疑是这样,您无法直接从 SDK 检索 Firebase 用户的列表,但您可以使用Firebase AdminCloud 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)或者(以及我建议的路径),您还可以向应用程序添加代码,以便在用户进行身份验证时,如果/users中不存在他们的用户节点,则创建它(使用从 Auth 参数返回的用户 uid 作为节点键)

You should call the val() method of the child DataSnapshot as follows:您应该按如下方式调用child DataSnapshotval()方法:

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?您的 Firebase 安全规则是什么样的?

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请参阅这些文档: https://firebase.google.com/docs/database/security#section-authorization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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