Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase update callback to detect error and sucssess

Tags:

firebase

How do I make this call back work? I have read the documents but I just cant figure it out for some reason?

   var ref = new Firebase("https://xxx.firebaseio.com/public/"+$scope.uid+"/shows/");
var blast = ref.child(show_title);

blast.update({
"show_title": show_title,
"show_image": show_image,
"show_description": show_description,
"show_email": show_email,
"time": Firebase.ServerValue.TIMESTAMP
});

 blast.update("I'm writing data", function(error) {
   if (error) {
    alert("Data could not be saved." + error);
  } else {
    alert("Data saved successfully.");
  }
});
like image 893
Bill Avatar asked Nov 09 '15 13:11

Bill


2 Answers

Frank's solution is perfect for your question. Another option is to use the update promise. It's particularly useful if you're doing a bunch of operations together which is often the case in Firebase.

here's an example using a promise

blast.update({ update: "I'm writing data" }).then(function(){
  alert("Data saved successfully.");
}).catch(function(error) {
  alert("Data could not be saved." + error);
});
like image 56
felipesk Avatar answered Oct 11 '22 13:10

felipesk


Your second call to update() raises this error in the JavaScript console:

Uncaught Error: Firebase.update failed: First argument must be an object containing the children to replace.(…)

The first argument to update has to be an object, so:

blast.update({ update: "I'm writing data" }, function(error) {
  if (error) {
    alert("Data could not be saved." + error);
  } else {
    alert("Data saved successfully.");
  }
});

For reference, this is the documentation for the update() function.

like image 30
Frank van Puffelen Avatar answered Oct 11 '22 13:10

Frank van Puffelen