Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.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 10小时后删除Firebase子级_Javascript_Firebase_Firebase Realtime Database_Google Cloud Functions_Firebase Admin - Fatal编程技术网

Javascript 10小时后删除Firebase子级

Javascript 10小时后删除Firebase子级,javascript,firebase,firebase-realtime-database,google-cloud-functions,firebase-admin,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,Firebase Admin,我想有一个功能,删除一个10小时大的孩子。到目前为止,我已经有了这段代码,但是如果我将其部署到Firebase云函数中,它会立即从数据库中删除所有数据。我不确定我做错了什么。请帮忙 exports.deleteOldItems = functions.database.ref('Rollerbanken/{pushId}') .onWrite(event => { var ref = event.data.ref.parent; // reference to the items

我想有一个功能,删除一个10小时大的孩子。到目前为止,我已经有了这段代码,但是如果我将其部署到Firebase云函数中,它会立即从数据库中删除所有数据。我不确定我做错了什么。请帮忙

exports.deleteOldItems = functions.database.ref('Rollerbanken/{pushId}')
.onWrite(event => {
  var ref = event.data.ref.parent; // reference to the items
  var now = Date.now();
  var cutoff = now - 10 * 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);
  });
});
我的Firebase数据库结构:

{
  "Rollerbanken" : {
    "-Ku_Ywh8wElDdwNqa0KW" : {
      "Extrainformatie" : "",
      "Latitude" : "51.8306880305073",
      "Longitude" : "5.90483402833892",
      "Staater" : "Staat er nog steeds",
      "Staaternietmeer" : "",
      "Stad" : "Nijmegen",
      "Tijd" : "19:50",
      "TijdControle" : "19:50",
      "TijdControleniet" : "",
      "TypeControle" : "Rollerbank",
      "TypeControle2" : "Rollerbank",
      "postKey" : "-Ku_Ywh8wElDdwNqa0KW",
      "timestamp" : 1506016223000
        }
      }
    }

代码中有
ref.orderByChild('timestamp')
,但数据中没有
timestamp
字段。这被解释为具有
null
时间戳,它实际上具有Firebase施加的最高优先级。因此,所有数据节点都会在指定的
截止日期之前排序,并被删除


要解决这个问题,您需要将
时间戳
字段添加到模式中。

您的代码中有
ref.orderByChild('timestamp')
,但数据中没有
时间戳
字段。这被解释为具有
null
时间戳,它实际上具有Firebase施加的最高优先级。因此,所有数据节点都会在指定的
截止日期之前排序,并被删除


要解决这个问题,您需要将
时间戳
字段添加到您的模式中。

这就是我24小时编码我的方式

 exports.deleteOldItems = functions.database.ref('/notification/{user_id}/{notification_id}').onWrite((change) => {

    const ref = change.after.ref.parent; // reference to the parent

    // For easier cross browser solution, get old time like this
    const yesterday = Date.now() - 86400000; // that is: 24 * 60 * 60 * 1000
    const oldItemsQuery = ref.orderByChild('time').endAt(yesterday);

    return oldItemsQuery.once('value').then((snapshot) => {
      // create a map with all children that need to be removed
      const updates = {};
          snapshot.forEach(child => {
                updates[child.key] = null;
          });
      return ref.update(updates);
      // execute all updates in one go and return the result to end the function
    });
  });

快乐编码

这就是我24小时编码的方式

 exports.deleteOldItems = functions.database.ref('/notification/{user_id}/{notification_id}').onWrite((change) => {

    const ref = change.after.ref.parent; // reference to the parent

    // For easier cross browser solution, get old time like this
    const yesterday = Date.now() - 86400000; // that is: 24 * 60 * 60 * 1000
    const oldItemsQuery = ref.orderByChild('time').endAt(yesterday);

    return oldItemsQuery.once('value').then((snapshot) => {
      // create a map with all children that need to be removed
      const updates = {};
          snapshot.forEach(child => {
                updates[child.key] = null;
          });
      return ref.update(updates);
      // execute all updates in one go and return the result to end the function
    });
  });

快乐编码

你能给我们看一个储存在
Rollerbanken/{pushId}
的项目的例子吗?当然,我编辑过它!你能给我们看一个储存在
Rollerbanken/{pushId}
的项目的例子吗?当然,我编辑过它!现在我的结构中有了时间戳:“timestamp”:ServerValue.timestamp(),但它仍然不起作用!我看了一下航海日志,上面写着“好的”。所以我不明白发生了什么?我现在在我的结构中有了时间戳:“timestamp”:ServerValue.timestamp(),但它仍然不工作!我看了一下航海日志,上面写着“好的”。所以我不明白发生了什么?