John Smith Optional
John Smith Optional

Reputation: 491

Remove node from Firebase with Functions

I'm trying to remove a node from Firebase using cronjob and i have this function but when it gets executed I get an error saying "Error: could not handle the request" and the log says: "database is not defined"

This is my function:

exports.cleanStatsOnRequest = functions.https.onRequest((req, res) => {
  const ref1 = firebase.database.ref;
  const dbref = ref1.child(`/dailystats`);
  console.log('removing dailystats');
  return dbref.remove
    .then(() => {
      res.send('dailystats removed');
    })
    .catch(error => {
      res.send(error);
    });
});

What am I doing wrong? What is the right way to define the database?

Upvotes: 0

Views: 1286

Answers (2)

Satish Kumar
Satish Kumar

Reputation: 601

Try this. database,ref and remove are functions. Read this guide.

Also you should not return dbref.remove() as remove() will return a promise.

exports.cleanStatsOnRequest = functions.https.onRequest((req, res) => {
const ref1 = firebase.database().ref();     // changes here
const dbref = ref1.child('/dailystats');
console.log('removing dailystats');
return dbref.remove()                      // changes here
    .then(() => {
        res.send('dailystats removed');
    })
    .catch(error => {
        res.send(error);
    });
});

Upvotes: 1

Jen Person
Jen Person

Reputation: 7546

You need to use the Firebase Admin SDK to access the Realtime Database from an HTTP trigger Cloud Function. This documentation shows you how to read from the database. This example shows writing to the database, which would be similar to deleting.

Upvotes: 1

Related Questions