Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase catch exception

I have the following code that is throwing me some firebase exception in the console if the data that I want to save to firebase is invalid. I want to catch it and display it to the screen in a controlled manner rather than finding out from console. I dont know why my .catch is not catching any of the firebase exceptions?

this.databaseService.saveCodesToFirebase(jsonFromCsv)
  .then(result => {
    this.alertService.alertPopup('Success', 'Code Updated')
  })
  .catch(error => {
    this.errorMessage = 'Error - ' + error.message
  })


saveCodesToFirebase(myObj: Object) {
    let ref = firebase.database().ref();

    let path = this.userService.getCurrentUser().companyId + '/codes/'
    let lastUpdatedPath = this.userService.getCurrentUser().companyId + '/lastUpdated/';

    var updates = {}

    updates[path] = jobObject;
    updates[lastUpdatedPath] = Math.round(new Date().getTime() / 1000);

    return ref.child('codes').update(updates);
}

EXCEPTION: Firebase.update failed: First argument contains an invalid key () in property 'codes.apple20170318.codes'. Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]"

screen shot

like image 865
ErnieKev Avatar asked Mar 25 '17 04:03

ErnieKev


People also ask

What is a try catch?

The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception.

How do you catch errors in firebase?

Stay organized with collections Save and categorize content based on your preferences. The Firebase Authentication SDKs provide a simple way for catching the various errors which may occur which using authentication methods. The SDKs for Flutter expose these errors via the FirebaseAuthException class.


1 Answers

There's not much to go on here but my best guess is that the object you're passing to saveCodesToFirebase() has keys that contain dots in them, like the one shown in the error message: jobCodes.apple20170318.codes.

If you want to keep this model you will have to sanitize that object to replace any invalid characters in its keys (and its children keys, recursively) before doing the update() operation.

When it comes to catching the exception, you'll have to use a try/catch block. The .catch() attached to the promise in this case is only useful to detect errors returned by the server, but here it's the update() method itself the one synchronously throwing the exception.

One possible approach would be like this:

try {
  this.databaseService.saveCodesToFirebase(jsonFromCsv)
    .then(result => {
      this.alertService.alertPopup('Success', 'Code Updated')
    })
    .catch(error => {
      this.errorMessage = 'Error - ' + error.message
    })
} catch (error) {
  this.errorMessage = 'Error - ' + error.message
}
like image 110
Josep Sayol Avatar answered Oct 19 '22 16:10

Josep Sayol