如何使用Firebase Cloud Function根据时间和用户ID删除数据库快照?

时间:2018-07-07 00:24:41

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

我有一个可以更新用户位置以及更新时间的应用程序,有一个错误只能在服务器端解决,在我的情况下为Firebase

错误是,如果用户在打开我的应用程序时失去连接,他最终会将自己的位置存储在数据库中

我尝试部署Cloud Function,每2个小时删除一次UserID,但实际上并没有删除数据快照

所以我想知道如何获取用户ID并每2小时删除一次有问题的数据快照

1PQq ...->用户ID 休息是众所周知的

datasnapshot example

这是我使用的代码

    exports.deleteOldItems = functions.database.ref('/driversAvailable/{uid}')
.onWrite(event => {
  var ref = event.data.ref.parent; // reference to the items
  var now = Date.now();
  var cutoff = now - 2 * 60 * 60 * 1000;
  var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
  return oldItemsQuery.once('value', function(snapshot) {
    // create a map with all children that need to be removed
    var updates = {};
    snapshot.forEach(function(child) {
      updates[child.key] = null
    });
    // execute all updates in one go and return the result to end the function
    return ref.update(updates);
  });
});

0 个答案:

没有答案