如何在向Firefire添加数据时不向Angularfire发送数据时发生错误? [英] How to Catch Error When Data is not Sent on Angularfire when adding data to firebase?
问题描述
$ $ $ $ $ $ $ $($ $ b $ Name:$ scope.username,
年龄:$ scope.newage,
联系人:$ scope.newcontact,
});
alert('保存到firebase');
我成功地将这些数据发送到我的firebase,但是如何捕获这些数据没有保存成功?任何想法?
编辑
$ scope.users。$ add({
名称:'Frank',
年龄:' 20',
联系人:$ scope.newcontact,
})。then(function(ref){
alert('Saved。');
})。catch (错误){
console.error(error); // or
console.log(error);
alert('Not Saved。');
});
连接到互联网时。 then()函数很好。在显示警报之前,它将等待这些数据保存在firebase中。
我想要的是告诉我数据没有保存。 catch错误函数不会触发,当我关闭我的互联网连接并提交这些数据。
当您调用 $ add()
它返回一个承诺。要检测数据保存的时间,请执行然后()
。要检测何时保存失败,请执行 catch()
:
var list = $ firebaseArray(ref);
list $ add({foo:bar})。then(function(ref){
var id = ref.key;
console.log(added record with id catch(function(error){
console.error(error); $ b);
list。$ indexFor(id); //返回数组中的位置
} $ b});
请参阅 add()
的文档。
更新
检测Firebase数据库时,由于没有网络连接而无法保存数据是一个非常不同的问题。在这种情况下不能保存是不是一个错误,而只是一个临时的条件。这个条件不适用于这个 set()
操作,而是适用于所有的读/写操作。出于这个原因,您应该通过检测连接状态:
var connectedRef = firebase.database()。ref(。info / connected);
$ b connectedRef.on(value,function(snap){
if(snap.val()=== true){
alert(connected);
} else {
alert(not connected);
}
});
通过监听 .info / connected
代码可以知道用户没有连接到Firebase数据库并根据您的应用程序的需要进行处理。
Im using angularfire to save data into my firebase. Here is a quick code.
$scope.users.$add({
Name:$scope.username,
Age:$scope.newage,
Contact: $scope.newcontact,
});
alert('Saved to firebase');
I am successful in sending these data to my firebase however how can I catch an error if these data are not saved successfully? Any ideas?
EDIT
So after implementing then() function.
$scope.users.$add({
Name:'Frank',
Age:'20',
Contact: $scope.newcontact,
}).then(function(ref) {
alert('Saved.');
}).catch(function(error) {
console.error(error); //or
console.log(error);
alert('Not Saved.');
});
When connected to the internet. The then() function is fine. It waits for those data to be saved in firebase before showing the alert. What I want is for it to tell me that data is not saved. catch error function is not firing when i am turning off my internet connection and submitting those data.
When you call $add()
it returns a promise. To detect when the data was saved, implement then()
. To detect when saving failed, implement catch()
:
var list = $firebaseArray(ref);
list.$add({ foo: "bar" }).then(function(ref) {
var id = ref.key;
console.log("added record with id " + id);
list.$indexFor(id); // returns location in the array
}).catch(function(error) {
console.error(error);
});
See the documentation for add()
.
Update
To detect when the data cannot be saved due to not having a network connection is a very different problem when it comes to the Firebase Database. Not being able to save in this case is not an error, but merely a temporary condition. This condition doesn't apply just to this set()
operation, but to all read/write operation. For this reason, you should handle it more globally, by detecting connection state:
var connectedRef = firebase.database().ref(".info/connected");
connectedRef.on("value", function(snap) {
if (snap.val() === true) {
alert("connected");
} else {
alert("not connected");
}
});
By listening to .info/connected
your code can know that the user is not connected to the Firebase Database and handle it according to your app's needs.
这篇关于如何在向Firefire添加数据时不向Angularfire发送数据时发生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!