Firebase 更新回调以检测错误和成功
Firebase update callback to detect error and sucssess
如何使回电有效?我已经阅读了文档,但出于某种原因我无法理解?
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.");
}
});
您对 update()
的第二次调用在 JavaScript 控制台中引发了此错误:
Uncaught Error: Firebase.update failed: First argument must be an object containing the children to replace.(…)
update
的第一个参数必须是一个对象,所以:
blast.update({ update: "I'm writing data" }, function(error) {
if (error) {
alert("Data could not be saved." + error);
} else {
alert("Data saved successfully.");
}
});
Frank 的解决方案非常适合您的问题。另一种选择是使用更新承诺。如果您同时执行一系列操作(这在 Firebase 中很常见),它会特别有用。
这是一个使用 promise 的例子
blast.update({ update: "I'm writing data" }).then(function(){
alert("Data saved successfully.");
}).catch(function(error) {
alert("Data could not be saved." + error);
});
在 React ES6 中:
var refTypes = db.ref("products").child(productId);
var updates = {};
updates[productId] = newProduct;
refTypes.update(updates).then(()=>{
console.log("Data saved successfully.");
}).catch((error)=> {
console.log("Data could not be saved." + error);
});
如何使回电有效?我已经阅读了文档,但出于某种原因我无法理解?
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.");
}
});
您对 update()
的第二次调用在 JavaScript 控制台中引发了此错误:
Uncaught Error: Firebase.update failed: First argument must be an object containing the children to replace.(…)
update
的第一个参数必须是一个对象,所以:
blast.update({ update: "I'm writing data" }, function(error) {
if (error) {
alert("Data could not be saved." + error);
} else {
alert("Data saved successfully.");
}
});
Frank 的解决方案非常适合您的问题。另一种选择是使用更新承诺。如果您同时执行一系列操作(这在 Firebase 中很常见),它会特别有用。
这是一个使用 promise 的例子
blast.update({ update: "I'm writing data" }).then(function(){
alert("Data saved successfully.");
}).catch(function(error) {
alert("Data could not be saved." + error);
});
在 React ES6 中:
var refTypes = db.ref("products").child(productId);
var updates = {};
updates[productId] = newProduct;
refTypes.update(updates).then(()=>{
console.log("Data saved successfully.");
}).catch((error)=> {
console.log("Data could not be saved." + error);
});