【问题标题】:Firebase Cloud function update all entries in databaseFirebase Cloud 功能更新数据库中的所有条目
【发布时间】:2019-02-25 00:23:15
【问题描述】:

我在 Firebase 数据库中有一堆 cmets,我想通过 Cloud Function 对 cme​​ts 进行一些更新(这是一个简化的示例,我将做一些需要 Cloud Function 的逻辑)。

我需要做的是遍历数据库中的所有 cmets,调整其评级节点,然后使用调整后的 cmets 更新数据库。

我花了很多时间研究这个,但我对 Cloud Functions 完全陌生,所以我很难弄清楚这一点。 我假设我想将所有 cmets(可能有数千个)的所有更改存储在数组或对象中,然后一次进行更新,而不是分别为每个评论进行更新?

顺便说一句,这段代码不起作用,我假设数组和返回是完全错误的。

exports.increaseRating = functions.database.ref('/comments/')
    .onUpdate((snapshot) => {   
     
        var updates = [];

        snapshot.before.forEach((element) => {
            var comment = element.val();
            comment.rating += 1000;
            updates.push(comment);
        });

        return updates;
    })

我用来更新一个条目的代码。我需要同时为所有 cmets 做同样的事情。

exports.increaseRating = functions.database.ref('/comments/{commentId}')
    .onUpdate((snapshot, context) => {

        const comment = snapshot.before.val();
        const newRating = comment.rating += 1000;       
       
        const now = new Date().getTime();
        if (comment.lastUpdate) {
            if (comment.lastUpdate > now - (30 * 1000)) {
                return null;
            }
        }
        
        return admin.database().ref(`/comments/${context.params.commentId}`).update({
            "rating": newRating,
            "lastUpdate": now
        })
    })

【问题讨论】:

  • 您显示的代码都不会更新数据库。请记住:如果您更新 /comments 下的任何内容,您的代码将再次触发自身,从而创建一个无限循环。
  • @frankvanpuffelen 谢谢弗兰克,请告诉我,我将如何更新数据库?我用我用来更新一个条目的代码更新了这个问题,我主要关心的是如何一次更新说 1000 cmets。

标签: javascript firebase express firebase-realtime-database google-cloud-functions


【解决方案1】:

如果你想更新所有子节点,你可以这样做:

var ref = firebase.database().ref("comments"); // or admin.database().ref("comments")
ref.once("value").then((snapshot) => {
  var updates = {};
  snapshot.forEach((commentSnapshot => {
    var comment = commentSnapshot.val();
    var newRating = comment.rating + 1000;
    updates[commentSnapshot.key+"/rating"] = newRating;
  });
  ref.update(updates);
})

这将为所有 cmets 执行单个多位置更新。请注意,与执行单独更新相比,性能优势非常小,因为Firebase pipelines the multiple requests over a single connection

还请注意,您应该将其放在 /comments 上的 Cloud Functions 触发器中,因为这将导致无限循环:每次写入 cmets 时,您的函数都会触发,它会更新cmets,再次触发该函数。

如果您在 Cloud Functions 中需要此功能,则需要使用 HTTP 触发的函数,该函数由 HTTP 调用而不是数据库写入触发。

exports.updateCommentRatings = functions.https.onRequest((req, res) => {
  var ref = admin.database().ref("comments")
  ref.once("value").then((snapshot) => {
    var updates = {};
    snapshot.forEach((commentSnapshot => {
      var comment = commentSnapshot.val();
      var newRating = comment.rating + 1000;
      updates[commentSnapshot.key+"/rating"] = newRating;
    });
    ref.update(updates).then(() => {
      res.status(200).send("Comment ratings updated");
    });
  })
})

然后,您可以使用 cron-job.org 等服务定期调用此 URL/函数。有关更多信息,请参阅Cloud Functions for Firebase trigger on time?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-17
    • 2021-09-07
    • 2023-03-09
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多