![](/img/trans.png)
[英]How do I iterate through all child nodes in firebase database using javascript?
[英]How to iterate a firebase database using javascript?
我需要迭代Firebase数据库的用户。 我为每个用户都有一个令牌,我想向数据库的每个用户发送一个通知。
我的数据库如下所示:
console.log('Sending a notification to all users');
var ref = functions.database.ref('/users');
for (var token in ref){
console.log('Hello from: '+ ref.token);
}
您的ref
只是对Firebase Realtime数据库中特定路径的参考。 您尚未查询,因此还没有任何东西。 尝试这个:
console.log('Sending a notification to all users');
var ref = functions.database.ref('/users');
// Query the database
ref.once('value').then((usersSnapshot) => {
// Get back all the user records and iterate through them.
usersSnapshot.forEach((userRecordSnapshot) => {
// I'm guessing your record id is the token?
const token = userRecordSnapshot.key
// To get the other values, for example
const email = userRecordSnapshot.val().email
});
});```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.