Illegal return statement in JavaScript

后端 未结 1 742
清酒与你
清酒与你 2021-01-27 13:16

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

相关标签:
1条回答
  • 2021-01-27 13:29
    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.

    0 讨论(0)
提交回复
热议问题