【问题标题】:For or While with Javascript and Firebase Realtime DatabaseFor or While 使用 Javascript 和 Firebase 实时数据库
【发布时间】:2022-09-29 02:31:04
【问题描述】:

Firebase 和 Javascript 可以做到这一点吗? 我有一个看起来像这样的实时数据库:

现在我想访问这个数据库并将这两个父字符串复制到一个数组中。 之后,我想要一个 for 或 while 循环,在该循环中执行下面的这个函数,但一次使用第一个字符串,第二次使用第二个字符串。

这是代码:

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

结果应该与我输入以下内容相同:

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

直到通知的路径是已知的。但我不知道通知中存储了多少个字符串。所以它需要得到所有这些。

我用 while 和 for 循环观看了this tutorial here,但它们适用于数组。

更新代码: 如果我必须像这样在我的数据库中添加多个东西而不是像上面那样只添加一行怎么办?

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

    标签: javascript node.js firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    序言:由于您使用 google-cloud-functions 标签标记了您的问题,我假设您使用的是非模块化 Admin SDK。

    如果我正确理解了您的问题,以下应该可以解决问题(使用 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);
        });
    

    我们使用Object.keys()获取snapshot.val()返回的Object的key,Promise.all()多次异步调用。


    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;
    

    最后,请注意,您可以使用update() 方法以原子方式将多个节点写入数据库,而不是使用Promise.all(),如文档中here 中所述(请参阅V8 代码版本,它对应于非-模块化管理 SDK 语法)。

    【讨论】:

    • 您好@submariner,您有时间查看建议的解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多