Cloud functions for handling Firebase errors

I am wondering how to write node.js code correctly when I received the promises chain, and I need to update the database in real time if something goes wrong?

Here is the code:

export const testErrorHandling = functions.database
      .ref('/workqueue/{pushId}/something').onWrite(event => {

  // Exit when the data is deleted.
  if (!event.data.exists()) {
    return;
  }

  //This is the retry count, give up if more than 5 times have been retried.
  const data = event.data.val()
  if (data.count >= 5) {
    return
  }

  return event.data.ref.root.child(data.fulluri).once('value').then(snapshot => {
    //Process all, if ok, delete the work queue entry
    return event.data.ref.remove()  
  }).catch(exception => {
    console.log('Error!: ' + exception)

    //Log error, increase retry count by one an write to that 
    //location to trigger a retry

    //Is the line below OK?
    //return event.data.ref.child('count').set(data.count + 1)
  })

})

I would suggest that this is a common requirement in many cases, but cannot find an example, since all the examples seem to just be written to console.error and executed. (What rarely happens in the real world.)

+4
source share
1 answer

[ Firebase] . , , - ( Promise.race, ).

. , . , / ? ?

+2

Source: https://habr.com/ru/post/1683672/


All Articles