36409

Firebase Cloud Functions deletes nodes directly after rather than 24 hours later

<h3>Question</h3>

My goal is to delete all the message nodes 24 hours after they were sent using Firebase Cloud Functions and the Realtime Database. I tried copy and pasting the answer from this post however for some reason the messages delete directly after they were created rather than the 24 hours later. If someone could help me solve this problem I would really appreciate it. I have tried multiple different answers based on the same issue and they haven't worked for me.

Here is my index.js file:

'use strict'; const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); // Cut off time. Child nodes older than this will be deleted. const CUT_OFF_TIME = 24 * 60 * 60 * 1000; // 2 Hours in milliseconds. exports.deleteOldMessages = functions.database.ref('/Message/{chatRoomId}').onWrite(async (change) => { const ref = change.after.ref.parent; // reference to the parent const now = Date.now(); const cutoff = now - CUT_OFF_TIME; const oldItemsQuery = ref.orderByChild('seconds').endAt(cutoff); const snapshot = await oldItemsQuery.once('value'); // create a map with all children that need to be removed const updates = {}; snapshot.forEach(child => { updates[child.key] = null; }); // execute all updates in one go and return the result to end the function return ref.update(updates); });

And my database structure is:


<h3>Answer1:</h3>

In the comments you indicated that you're using Swift. From that and the screenshot it turns out that you're storing the timestamp in seconds since 1970, while the code in your Cloud Functions assumes it is in milliseconds.

The simplest fix is:

// Cut off time. Child nodes older than this will be deleted. const CUT_OFF_TIME = 24 * 60 * 60 * 1000; // 2 Hours in milliseconds. exports.deleteOldMessages = functions.database.ref('/Message/{chatRoomId}').onWrite(async (change) => { const ref = change.after.ref.parent; // reference to the parent const now = Date.now(); const cutoff = (now - CUT_OFF_TIME) / 1000; // convert to seconds const oldItemsQuery = ref.orderByChild('seconds').endAt(cutoff); const snapshot = await oldItemsQuery.once('value'); // create a map with all children that need to be removed const updates = {}; snapshot.forEach(child => { updates[child.key] = null; }); // execute all updates in one go and return the result to end the function return ref.update(updates); });

Also see my answer here: How to remove a child node after a certain date is passed in Firebase cloud functions?

来源:https://stackoverflow.com/questions/56317618/firebase-cloud-functions-deletes-nodes-directly-after-rather-than-24-hours-later

Recommend

  • Convert a key-value array with duplicate keys into array of object with unique key, and value array
  • How to parse huge xml data from webservice in Android application? [closed]
  • Distinct Count across Bigquery arrays
  • Get Skype voice notes in a bot
  • Task queue for wp8?
  • best simplest code to move a UIImage View from point A to point B
  • Python + Tkinter Windows 7 taskbar progress
  • How to use jeromq in MATLAB
  • kube-apiserver high CPU and requests
  • OWL Api, move class from parent A to B
  • C99′s Fixed-Width Integer Types
  • wordpress posts_orderby filter with custom table in plugin
  • Simple Donut Chart with Raphael
  • How to add a CAGradientLayer to a UIView when using programmatic constraints
  • combining pandas dataframes of different sampling rates
  • How to open a serial port with pyserial?
  • Calling UDF on Dataframe with Serialization Issue
  • Trying to get lotusscript json reader
  • How can I detect mongodb reconnect failed event
  • Running iPhone crash Logs from testers on XCode
  • How can I run an Illustrator javascript on all files in a directory?
  • Do iTunesConnect external testers get updates for *every* uploaded ipa file after Beta Approval?
  • Use awk to convert GPS Position to Latitude & Longitude
  • Smarter Removing Unnecessary WhiteSpace CSV
  • Authorize Attribute Authentication with Postman in Web Api
  • Adding native code to an existing Worklight hybrid app
  • playing mp3 from nsbundle
  • Swift manually rotate view controller
  • 'Edit' function for forum posts and such
  • How to write seo friendly url's using htaccess?
  • Create an average of multiple excel chart without the data source
  • how to run ejabberd with Erlang on Heroku?
  • How to use Streams api peek() function and make it work?
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Google App Engine backend servlet not responding
  • Make checkout phone field optional for specific countries in WooCommerce
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007