繁体   English   中英

如何使用javascript迭代Firebase数据库?

[英]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.

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