0

A 'days' node with more than 1 child isn't getting removed. How can I fix this issue?

I need to ensure that my promise bubbles up to the last then() on the top-level. So I need a return before collectionRef.once. But that return statement now prevents the collectionRef.once from happening. I'm stuck!

Here's my code

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

exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}')
.onWrite(event => {
  var ref = event.data.ref.parent; // reference to the items
  var now = Date.now();
  var cutoff = now - 2 * 60 * 60 * 1000;
  var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
  return oldItemsQuery.once('value', 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);
  }).then(function() {;

    const theRef = event.data.ref;
    const collectionRef = theRef.parent.child('days');
    return collectionRef; // ILEGAL RETURN STATEMENT
    collectionRef.once('value').then(messagesData => {
        if(messagesData.numChildren() > 1) {

  let updates = {};
updates['/days'] = null;
return defaultDatabase.ref().update(updates); // 'days' doesn't get removed even if it has more than 1 child (as in the image)!
        }
    })
});

});

Data structure: https://i.stack.imgur.com/gVn8S.jpg

  • Can you please stop duplicating questions, stick to one topic, no need to spam with same [question](https://stackoverflow.com/questions/44743530/node-with-more-than-1-child-is-not-getting-removed) – Mihai Iorga Jun 30 '17 at 07:20
  • @MihaiIorga Nobody's answering. I'll accept the correct answer. –  Jun 30 '17 at 07:31

1 Answers1

0
exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}')
  .onWrite(event => {
    var ref = event.data.ref.parent // reference to the items
    var now = Date.now()
    var cutoff = now - 2 * 60 * 60 * 1000
    var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff)
    return oldItemsQuery.once('value', 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)
    }).then(function() {
      // const theRef = event.data.ref
      const collectionRef = defaultDatabase.ref().child('/days')
      // return collectionRef // ILEGAL RETURN STATEMENT
      collectionRef.once('value').then(messagesData => {
                console.log(`Hello messageData : ${messagesData.numChildren()}`)
              if(messagesData.numChildren() > 1) {
                    const updates = {}
                    updates['/days'] = null
                    return defaultDatabase.ref().update(updates); // 'days' doesn't get removed even if it has more than 1 child (as in the image)!
                }
      })
  })

use defaultDatabase.ref().child('/days') instead of using event.data.ref.parent

also please go through the documentation and learn how promises works it will help you future. For now these changes will work.Tested at my end.

videos you must watch

What's the difference between event.data.ref and event.data.adminRef? - #AskFirebase Asynchronous Programming (I Promise!) with Cloud Functions for Firebase - Firecasts

you can subscribe their Firebase YouTube Channel to get latest updates and learn More.

  • Great answer. Accepted! But, but how do I check if `const collectionRef = defaultDatabase.ref().child('/days')` exists? –  Jun 30 '17 at 12:16
  • If it exists at the moment of `.onWrite`, I want to remove 'days'. If it doesn't at the moment of the `.onWrite`, I don't want 'days' to be removed. –  Jun 30 '17 at 12:19
  • i didn't get your question but i will try to answer it . you cannot remove something which doesn't exist so you dont have to worry about removing something that doesn't exist. – Ashutosh Barthwal Jul 01 '17 at 04:00