Cloud Functions for Firebase 被多次调用

Cloud Functions for Firebase invoked many times

我只是在试用 Cloud Functions for Firebase,以便将我的 firebase-queue 工作人员转移到云功能上。每当我在给定的引用处创建新节点时,我添加了一个简单的函数来添加最后更新的时间戳。该函数如下所示:

var functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.setLastUpdatedTimestamp = functions.database.ref('/nodes/{nodeId}')
  .onWrite(event => {
    const original = event.data.val();
    console.log('Adding lastUpdatedTimestamp to node ', original.name);
    return event.data.ref.child('lastUpdatedAtFromFC').set(Date.now());
  });

我部署了这个云函数并从我的应用程序中添加了一个节点。我去了 Firebase Functions 仪表板,看到该函数已被调用 169 次,我不知道为什么。当我查看日志时,我也看到类似将函数附加到所有过去节点的日志。

onWrite 的行为是否有点像 child_added 和 运行 所有现有实体的功能?

我每次更改并再次部署该功能时,都会重复此操作吗?

我原以为新添加的节点会 运行 一次。

这是编写处理数据库写入的函数时的常见错误。当您处理某个位置的初始写入事件,然后对同一位置进行第二次写入时,第二次写入将触发另一个事件,该事件将再次 运行 函数,依此类推,这将是无限循环。

您的函数中需要一些逻辑来确定第二个写入事件是否不应重新写入数据库。这将停止循环。在您的情况下,您不需要设置上次更新时间的功能。您可以使用客户端上的特殊值来告诉服务器将当前时间插入字段。

https://firebase.google.com/docs/reference/js/firebase.database.ServerValue#.TIMESTAMP

道格是正确的。此外,最好知道如果您有一个函数陷入无限循环,让它停止的方法是重新部署您的函数(使用 firebase deploy)并修复循环,或者删除整个功能(通过将其从 index.js 和 运行 firebase deploy 中删除)。

我确实遇到了这个问题。这里有两个问题。如何在无限循环开始后停止它。已经回答了。但真正的问题是如何使用 Firebase Cloud Function 触发器将 lastUpdated 日期字段添加到您的对象。

这是我处理 onWrite() 循环问题的尝试。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();


exports.onWriteFeatherUpdateSummary = functions.database.ref('/messages/{id}')
    .onWrite((change, context) => {
      // Grab the current value of what was written to the Realtime Database.
      let updated = change.after.val();
      // Grab the previous value of what was written to the Realtime Database.
      const previous = change.before.val();
      let isChanged = true;
      let isCreated = (previous === null); // object 'created'

      // Only when the object gets updated
      if (!isCreated) {
        // An object should never directly change the lastUpdated value, this is for trhe trigger only
        isChanged = (updated.lastUpdated === previous.lastUpdated);
      }

      console.log(`isChanged: ${isChanged} ; isCreated: ${isCreated}`);

      if(isChanged) {
        // Insert whatever extra data you wnat on the update trigger
        const summary = `This is an update!`;

        // Add a 'createdDate' field on the first trigger
        if (isCreated) {
          // Make sure your object has a createdDate (Date) before the lastUpdated (Date)!
          Object.assign(updated,
              {
                createdDate : admin.database.ServerValue.TIMESTAMP
              }
            );
        }

        // Add lastUpdated Date field on very update trigger (but not when you just changed it with the trigger!)
        Object.assign(updated,
            {
              summary : summary,
              lastUpdated : admin.database.ServerValue.TIMESTAMP
            }
          );
      }

      return change.after.ref.set(updated);
    });