Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除旧的firebase数据_Javascript_Firebase_Firebase Realtime Database_Google Cloud Functions - Fatal编程技术网

Javascript 删除旧的firebase数据

Javascript 删除旧的firebase数据,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我想删除超过2小时的数据。我尝试了另一个答案,但没有解决我的问题 现在,让我展示我的代码和firebase结构 火基结构: Index.js: const functions = require('firebase-functions'); exports.deleteOldItems = functions.database.ref('Room/English/{pushId}') .onWrite(event => { var ref = event.data.ref.par

我想删除超过2小时的数据。我尝试了另一个答案,但没有解决我的问题

现在,让我展示我的代码和firebase结构

火基结构:

Index.js:

const functions = require('firebase-functions');
exports.deleteOldItems = functions.database.ref('Room/English/{pushId}')
.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('time').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);

  });
});
然后,我将这个js文件部署到我的firebase

firebase deploy --only functions
部署成功,终端显示“部署完成!”。我可以在Firebase函数中看到deleteOldItems函数


因此,我的结构和代码是这样的。当向Room/English节点添加新数据时,不会发生任何更改。此节点中的数据(早于2小时)不会被删除。我怎样才能解决这个问题?我犯了什么错

你为什么喜欢这个?回答错了!我的孩子是时间,而且我认为,我的路径是Room/English/{pushId}云函数的签名已经改变。我更新了我的原始答案,并将再次结束你的问题。请注意,此处记录了这一变化:并且还更新了回购协议样本:。非常感谢,Puffelen先生。你解决了我的问题:)祝你今天愉快!
exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}')
.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);
  });
});