简体   繁体   English

对于或同时使用 Javascript 和 Firebase 实时数据库

[英]For or While with Javascript and Firebase Realtime Database

Is this possible with Firebase and Javascript?这对 Firebase 和 Javascript 是否可行? I have a realtime Database which looks like this:我有一个看起来像这样的实时数据库: P1

Now I want to get access to this database and copy these two parent string into an array.现在我想访问这个数据库并将这两个父字符串复制到一个数组中。 After that I want a for or while loop where this function below gets executed but one time with the first string and the second time with the second string.之后,我想要一个 for 或 while 循环,在该循环中执行下面的 function,但一次使用第一个字符串,第二次使用第二个字符串。

This is the code:这是代码:

var ref = db.ref('/user/' + >>Place String<< + '/notification/'+ subMessageID + '/subMessageID/').set(subMessageID);

The result should be the same as if I would have typed this:结果应该与我输入以下内容相同:

var ref = db.ref('/user/' + 'PpRWBqHFYlg78vSeutNOAPMG3wv1' + '/notification/'+ subMessageID + '/subMessageID/').set(subMessageID);
var ref = db.ref('/user/' + 'hSzkimAs1fWgNke9stnQnf7cNyi2' + '/notification/'+ subMessageID + '/subMessageID/').set(subMessageID);

The path till notification is known.直到通知的路径是已知的。 But I don't know how many strings are stored in notification.但我不知道通知中存储了多少字符串。 So it needs to get all of them.所以它需要得到所有这些。

I watched this tutorial here with while and for loops but they work with arrays.我在这里使用 while 和 for 循环观看了本教程,但它们与 arrays 一起使用。

Preamble: Since you tagged your question with the google-cloud-functions tag, I make the assumption you are using the the non-modular Admin SDK.序言:由于您使用google-cloud-functions标签标记了您的问题,我假设您使用的是非模块化管理员 SDK。

If I correctly understand your question, the following should do the trick (using the Admin SDK in a Cloud Function):如果我正确理解您的问题,以下应该可以解决问题(在云函数中使用 Admin SDK):

  db.ref('placeID/here:pds:.../-NCe5.../notification')
    .get()
    .then((snapshot) => {
      if (snapshot.exists()) {
        const keyArray = Object.keys(snapshot.val());
        // We pass an Array of Promises to Promise.all(), see the doc (link below)
        return Promise.all(keyArray.map(k => db.ref('/user/' + k + '/notification/' + subMessageID + '/subMessageID/').set(subMessageID)));
      } else {
        console.log('No data available');
        return null;
        // Or throw an error, it's up to you to decide depending on the desired flow of actions
      }
    })
    .then(() => {
       return null;  // See https://firebase.google.com/docs/functions/terminate-functions !!IMPORTANT when writing a CF
    })
    .catch((error) => {
      console.error(error);
    });

We use Object.keys() to get the keys of the Object returned by snapshot.val() and Promise.all() to call several times the asynchronous.我们使用Object.keys()获取snapshot.val()和Promise.all()返回的Object的keys调用多次asynchronous.all()


The async / await version would be: async / await版本将是:

  const snapshot = await db.ref('placeID/here:pds:.../-NCe5.../notification').get();
  if (snapshot.exists()) {
        const keyArray = Object.keys(snapshot.val());
        await Promise.all(
        keyArray.map((k) =>
            db
            .ref(
                '/user/' +
                k +
                '/notification/' +
                subMessageID +
                '/subMessageID/'
            )
            .set(subMessageID)
        )
        );
  } else {
        console.log('No data available');
  }
  return null;

Finally, note that instead of using Promise.all() you could use the update() method for writing in an atomic manner several nodes to the DB, as explained here in the doc (see the V8 code version, which corresponds to the non-modular Admin SDK syntax).最后,请注意,您可以使用update()方法以原子方式将多个节点写入数据库,而不是使用Promise.all() 如文档中所述(请参阅 V8 代码版本,它对应于非-模块化管理员 SDK 语法)。

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

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