Firebase更新回调以检测错误和sucssess

Bil*_*ill 10 firebase

如何使此回叫工作?我已阅读过这些文件,但由于某些原因我无法弄明白?

   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.");
  }
});
Run Code Online (Sandbox Code Playgroud)

小智 13

弗兰克的解决方案非常适合您的问题.另一种选择是使用更新承诺.如果你一起做一堆操作,这在Firebase中通常就是这种情况,这是特别有用的.

这是一个使用承诺的例子

blast.update({ update: "I'm writing data" }).then(function(){
  alert("Data saved successfully.");
}).catch(function(error) {
  alert("Data could not be saved." + error);
});
Run Code Online (Sandbox Code Playgroud)

  • 这确实也会奏效.请注意,当提出问题时(我已回答),[Firebase JavaScript SDK尚未支持承诺](https://firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html ). (5认同)

Fra*_*len 8

您的第二次调用update()会在JavaScript控制台中引发此错误:

未捕获的错误:Firebase.update失败:第一个参数必须是包含要替换的子项的对象.(...)

第一个参数update必须是一个对象,所以:

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

作为参考,这是update()功能文档.