1

I'm using the following code to set up a listener for Firebase Database Ref:

export function listenToUserEventsFeed (userId, cb, errorCB) {
  database.ref(`proUserEvents/${userId}`).on('value', (snapshot) => {
    console.log('SNAPSHOT RECEIVED')
    const feed = snapshot.val() || {}
    const sortedIds = Object.keys(feed).sort((a, b) => feed[b].createdAtTimeStamp - feed[a].createdAtTimeStamp)
    cb({feed, sortedIds})
  }, (error) => {
    console.log('SNAPSHOT ERROR: ', error)
  })
}

But the console.log('SNAPSHOT ERROR: ', error) never runs if I test using no internet connection. Am I missing something or is there something wrong in my code? I essentially want to pass down the error to the errorCB() function.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
jasan
  • 11,475
  • 22
  • 57
  • 97

1 Answers1

1

The error callback will only be called in case of an error, i.e. when your current client has no permission to read the data it is trying to read.

Not having an internet connection is not an error.

If you want to detect whether there is an internet connection, listen for .info/connected.

Related:

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807