Goeken
Goeken

Reputation: 95

Firebase Cloud Function in Javascript. Remove() function not working

I have a Firebase cloud function. Everything works as expected within the helloWorld function except the line deedRef.limitToLast(1).remove(); I also tried to do .ref(/deeds/${deedID}).remove() is there a reason why I can't remove data from firebase within cloud functions? The output from the http request is "Error: could not handle the request".

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const deedRef = admin.database().ref('/deeds');
const oldDeedRef = admin.database().ref('/oldDeeds');
exports.helloWorld = functions.https.onRequest((req, res) => {

    deedRef.limitToLast(1).once("value", (snapshot) => {
                snapshot.forEach((deedSnapshot) =>{
                    let deedID = deedSnapshot.val().id;
                    let text = deedSnapshot.val().message;
                    oldDeedRef.push({
                        id: deedID,
                        message: text
                    })
                })
            })
    deedRef.limitToLast(1).remove();
    res.send("Congrats For running the function");
});

Upvotes: 0

Views: 288

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

The problem has nothing to do with Cloud Functions.

deedRef.limitToLast(1) returns a Query type object. Query doesn't have a method called remove(). Therefore, your code will fail at runtime with a message to that effect.

If you want to delete some data from Realtime Database, you're going to need a Reference type object, which has a remove() method. This will remove everything at the location of the reference.

Upvotes: 2

Related Questions