Skip to content Skip to sidebar Skip to footer

Write To Firebase Database From Cloud Function Triggered By PubSub

I'm trying to use a daily trigger to alter information in my Firebase database. I have a number of other cloud functions that work properly, but I can't seem to get this function t

Solution 1:

I found the answer to the problem. I had initialized the app according to Firebase's documentation on push notifications:

admin.initializeApp({


  credential: admin.credential.cert({
    projectId: {projectId},
    clientEmail: {email},
    privateKey: {private key}
  }),
 databaseURL: {databaseURL}
 });

But for some reason this caused issues. I switched it out with a simple

admin.initializeApp(functions.config().firebase);

This seems to have fixed the issue, and push notifications are still working.


Solution 2:

The documentation explains that a function that performs asynchronous processing must return a JavaScript Promise:

Resolve functions that perform asynchronous processing by returning a JavaScript promise

Change your code to this:

exports.daily_job = functions.pubsub.topic('daily-tick').onPublish((event) => {
  console.log("This job is run every day!");
  const databaseRef = admin.database().ref('/accountActions/dailyDeletion');
  return databaseRef.child('delete').set("Data has been deleted!").then(() => {
    console.log('End of database clearing');
  }).catch(error => {
    console.error('Database clearing failed: ', error);
  });
});

Solution 3:

Insert this command somewhere before exports.daily_job..

admin.initializeApp(functions.config().firebase);

Post a Comment for "Write To Firebase Database From Cloud Function Triggered By PubSub"