【发布时间】:2019-11-24 05:34:51
【问题描述】:
我正在尝试获取服务器的日期时间值,并且我已经实现了下面的代码。但是,时间的返回值有时为空,有时无法按预期工作。
https请求代码的主要原因是检查代码今天是否已经运行,以免重复其操作。
export const helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!")
const database = ref.child('Admins/currentDate')
const timeStamp = admin.database.ServerValue.TIMESTAMP
const currentTime = new Date(timeStamp)
const currentDate = currentTime.getDate().toString
const currentMonth = currentTime.getMonth().toString
const currentYear = currentTime.getFullYear().toString
const dateFormat = currentDate + " " + currentMonth + " " + currentYear
database.once('value').then(function (snapshot) {
snapshot.forEach(function (data) {
const appTime = data.val().currentTime
if(appTime === dateFormat){
console.log("Points already added today." + dateFormat)
}
else{
console.log("All points successfully updated." + dateFormat)
data.ref.update({currentTime : dateFormat})
.catch(console.error)
udpateAllPoints()
}
})
}).catch(console.error)
})
注意:updateAllPoints() 用于将所有用户点数增加 10% [此代码按预期工作]
function udpateAllPoints(){
const database = ref.child('Users')
database.once('value').then( function (snapshot){
snapshot.forEach(function (data){
const points = data.val().points
const updatedPoints = updatePoints(points)
const userId = data.key
data.ref.update({ points : updatedPoints})
.catch(console.error)
console.log("This User "+ userId +"has "+points+ "points. \n")
})
}).catch(console.error)
}
dateFormat 的当前示例输出如下所示:
-
上述代码运行时
function toString() { [native code] } function toString() { [native code] } function toString() { [native code] } -
当删除 toString
function { [native code] } function { [native code] } function { [native code] } -
当使用 Date.Now() 或 new Date() 而不是 new Date(timeStamp) 时
NaN NaN NaN
【问题讨论】:
标签: typescript firebase-realtime-database google-cloud-functions