Illegal return statement in JavaScript

2020-05-01 15:51发布

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

1条回答
家丑人穷心不美
2楼-- · 2020-05-01 16:28
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.

查看更多
登录 后发表回答