Javascript 10小时后删除Firebase子级
我想有一个功能,删除一个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
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(),但它仍然不工作!我看了一下航海日志,上面写着“好的”。所以我不明白发生了什么?