Reputation: 1095
I'm new to Firebase Cloud Functions. How can I auto-delete messages in Realtime Database after a certain time? For exemple, 1 minute,1 day etc.
I'm trying to use the sample available here (https://github.com/firebase/functions-samples/tree/master/delete-old-child-nodes) and getting the following error after using the firebase deploy command:
i deploying functions Running command: npm --prefix "$RESOURCE_DIR" run lint
functions@ lint /home/vitor/remove_msgs_teste/functions eslint .
/home/vitor/remove_msgs_teste/functions/index.js 29:111 error Parsing error: Unexpected token =>
✖ 1 problem (1 error, 0 warnings)
npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! functions@ lint: eslint . npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the functions@ lint script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in: npm ERR! /home/vitor/.npm/_logs/2019-04-15T12_51_56_231Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code1
My index.js:
'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 = 30000; // 30sec in milliseconds.
/**
* This database triggered function will check for child nodes that are older than the
* cut-off time. Each child needs to have a `timestamp` attribute.
*/
exports.deleteOldItems = functions.database.ref('/mensagens/{idone}/{idtwo}/{pushid}').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('timestamp').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);
});
My Database:
How to solve this error?
Upvotes: 0
Views: 977
Reputation: 599571
It looks like you're trying to deploy code that require Node 8 (since it uses =>
notation) to an environment that doesn't support it.
On solution is to upgrade the environment to support Node 8. The alternative is to modify the code to not require Node 8 anymore, which can be done with:
exports.deleteOldItems = functions.database.ref('/mensagens/{idone}/{idtwo}/{pushid}').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);
});
});
This type of rewrite is fairly common in modern JavaScript, so I assume you're new to this. If you are new to JavaScript, Cloud Functions for Firebase is not the best way to learn it. I recommend first reading the Firebase documentation for Web developers and/or taking the Firebase codelab for Web developer. They cover many basic JavaScript, Web and Firebase interactions. You could also use the Admin SDK in a local Node.js process, which can be debugged with a local debugger. After those you'll be much better equipped to write code for Cloud Functions too.
Upvotes: 2