I want to automatically create an expiration timestamp for new items that are added to the Firebase database. So I wrote the following function:
exports.onCreateNewItem = functions.database.ref('/items/{item_id}').onCreate(event => {
var mergedUpdate = {};
mergedUpdate['expiration'] = admin.database.ServerValue.TIMESTAMP + 2678400000;
return event.data.adminRef.update(mergedUpdate);
})
However, it doesn't work. The value in /items/{item_id}/expiration
is "[object Object]2678400000"
while I expected it to be the timestamp of 31 days from now.
Because your Cloud Function code runs on a Google server, you have access to the server time via Date.now() . You don't need to use admin.database.ServerValue.TIMESTAMP
.
exports.onCreateNewItem = functions.database.ref('/items/{item_id}').onCreate(event => {
var mergedUpdate = {};
mergedUpdate['expiration'] = Date.now() + 2678400000;
return event.data.adminRef.update(mergedUpdate);
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.