![](/img/trans.png)
[英]Cloud Function deletes node instantly instead of after two hours based off timestamp (delete-old-child-nodes not working)
[英]Firebase Cloud Functions is deleting nodes instantly instead of deleting after 2 hours
我正在使用Cloud Functions在Firebase上2小時后刪除節點,但是,當我添加一個節點時,一旦在數據庫中創建該節點就將其刪除
我的index.js:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const CUT_OFF_TIME = 2 * 60 * 60 * 1000; // 2 Hours in milliseconds
exports.deleteOldItems = functions.database.ref('/posts/{randomID1}/{randomID2}/timestamp')
.onWrite(function(change) {
var ref = change.after.ref.parent; // reference to the parent
var now = Date.now();
var cutoff = now - CUT_OFF_TIME;
var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
return oldItemsQuery.once('value').then(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);
});
});
我的數據庫結構:
"posts" : {
"randomID1" : {
"randomID2" : {
"timestamp" : 1557842159
}
},
"randomID3" : {
"randomID4" : {
"timestamp" : 1557842203
}
},
您的時間戳以秒為單位存儲,而不是以毫秒為單位。 由於您的代碼使用Date.now
,它返回以毫秒為單位的時間戳,因此您正在比較的值是1000倍偏移。
最簡單的解決方案是:
const CUT_OFF_TIME = 2 * 60 * 60; // 2 Hours in seconds
您正在比較以毫秒為單位的時間戳和以秒為單位的時間戳。
var cutoff = now - CUT_OFF_TIME;
-截止值以毫秒為單位
您需要更改CUT_OFF_TIME
並將Date.now()
轉換為秒。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.