Firebase Cloud功能可删除过期的帖子

时间:2017-11-30 07:08:51

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

我是云端功能以及JavaScript的新手,并且很难实现以下功能。我正在尝试运行一个云函数,它从我的数据库中读取时间戳,并删除整个帖子/节点,如果它们超过3分钟。我有下面的代码但每次运行它都没有任何反应。它总是返回“ok”但没有被删除:

    const CUT_OFF_TIME = 180000;
    var dbRef = admin.database().ref('/open_windows/{pushId}');
    const now = Date.now();
    const cutoff = now - CUT_OFF_TIME;
    const oldItemsQuery = dbRef.orderByChild('timestamp').endAt(cutoff);

    oldItemsQuery.once('value').then(snapshot => {
        // create a map with all children that need to be removed
        const updates = {};
        snapshot.forEach(child => {
          console.log("adding key: " + child.key)
          updates[child.key] = null;
        });

        // execute all updates in one go and return the result to end the function
        dbRef.update(updates);
        res.status(200).send('ok');
      });

    });

我的数据库具有以下结构: enter image description here

有人可以帮我,告诉我哪里出错了。感谢

2 个答案:

答案 0 :(得分:0)

这可能与回答here的情况类似。尝试:

return dbRef.update(updates);

答案 1 :(得分:0)

我接下来的教程让我陷入了困境。 {pushId}是问题,我将其删除,现在一切正常。

var dbRef = admin.database().ref('/open_windows');