如何在Firebase Cloud Functions中传递特定日期后删除子节点?



所以我有一个数据库结构,我保存一个带有时间戳值的事件,该时间戳值指示事件何时过期并被删除

所以我创建了一个Firebase Cloud函数,我将现在的时间(Date((.now(与数据库中的时间戳进行比较。

问题是当代码运行时,事件会立即被删除

这是代码:

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
exports.removeOldMessages = functions.https.onRequest((req, res) => {
const messagesRef = admin.database().ref('events')
messagesRef.once('value', (snapshot) => {
snapshot.forEach((child) => {
child.forEach((child) => {
if (Number(child.val()['endDate']) >= new Date().getTime()) {
child.ref.set(null)
}
})
})
})
return res.status(200).end()
})
{   "events" : {
"N5iTuYzAbJa02RauxCl3uh2Nggz1" : {
"-LNmIvSdrwK96KCGcmXm" : {
"addedBy" : "Riyadh Figures",
"coordinate" : [ 24.70914690943994, 46.78851541131735 ],
"endDate" : "1538442801.0",
"imagePath" : "-LNmIvSdrwK96KCGcmXm",
"key" : "-LNmIvSdrwK96KCGcmXm",
"title" : "hjihgf",
"userPicture" : "N5iTuYzAbJa02RauxCl3uh2Nggz1"
}
}   },   "user_profiles" : {
"N5iTuYzAbJa02RauxCl3uh2Nggz1" : {
"email" : "riyadhfigures.official@gmail.com",
"name" : "Riyadh Figures",
"profile_picture" : "https://lh6.googleusercontent.com/-HfLRYyyTAxQ/AAAAAAAAAAI/AAAAAAAAAAA/AAN31DVNc5koC_GGuww6hxcMKPkx4niY-A/s96-c/photo.jpg"
},
"ah00Fe5hHnQM8tyceLE1xpWznUw1" : {
"email" : "lenepouv@gmail.com",
"name" : "Mbm Al Osaimi",
"profile_picture" : "https://lh6.googleusercontent.com/-M0JCv0lMNTE/AAAAAAAAAAI/AAAAAAAAAIU/0M2Cef0YLOU/s96-c/photo.jpg"
},
"m1zj6gDEoUa9aCdPcazrb0rTuFj2" : {
"email" : "mb.osaimi@gmail.com",
"name" : "Mosab Al Osaimi",
"profile_picture" : "https://lh5.googleusercontent.com/-X5va1C-uNpU/AAAAAAAAAAI/AAAAAAAAAAc/GVSZLwv46U0/s96-c/photo.jpg"
}   } }

Swift 使用以秒为单位的间隔/时间戳,小数部分表示亚秒级详细信息。大多数其他平台使用毫秒。这意味着这些值之间存在 1000 倍的差异,这解释了为什么您的比较不起作用。

最简单的解决方法是乘数或除以 1000。

例如
if (1000*Number(child.val()['endDate']) >= new Date().getTime()) {
child.ref.set(null)
}

最新更新