Это должно решить вашу проблему прямо сейчас, но, как упомянул Фрэнк ван Пуффелен в своих комментариях, изучение концепций обещаний очень поможет вам в будущем!

вопрос является точной копией:

Неверное возвращение в JavaScript 1 ответ

Узел 'days' с более чем одним дочерним элементом не удаляется. Как я могу исправить эту проблему?

Вот мой код ниже (родом изВот):

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;
    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)!
        }
    })
});

});

Структура данных:

Ответы на вопрос(1)

Ваш ответ на вопрос