Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to catch error in firestore when no internet

I recently updated my app from firebase to firestore but stuck at offline persistance. I am using react-native-firebase to integrate firestore and disable its perisistance but still not getting any catch error when no internet. This is my code to delete data but catch never gets error when no internet not is the promise resolved.

firebase.firestore().collection('d').doc(deviceid).delete().then(function () {
                                    console.log("Device Deleted");
                                    that.setState({
                                        loading: false
                                    });
                                    Toast.show('Device Deleted Succesfully', {
                                        duration: Toast.durations.SHORT,
                                        position: Toast.positions.TOP,
                                        shadow: true,
                                        animation: true,
                                        hideOnPress: true,
                                        delay: 0,
                                    });

                                }).catch(function (err) {
                                    console.log(err);
                                    that.setState({
                                        loading: false
                                    });
                                })
like image 226
Honney Goyal Avatar asked May 26 '18 13:05

Honney Goyal


People also ask

Can firestore work offline?

When you initialize Cloud Firestore, you can enable or disable offline persistence: For Android and Apple platforms, offline persistence is enabled by default.

Can I use firebase without Internet?

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.

Does firestore Create collection if not exists?

If either the collection or document does not exist, Cloud Firestore creates it.


1 Answers

Make your own helper function for deletion that throws an error if there is no Internet connection. You can make the API cleaner by not calling .collection() but rather passing the full path d + deviceid to it.

function onlineOnlyDelete(path) {
  if(!deviceApi.hasInternet()) throw 'No Internet, no delete'
  return firebase.firestore().doc(path).delete()
}

replace firebase.firestore().collection('d').doc(deviceid) with onlineOnlyDelete(d+deviceid) and you should be good to go

like image 73
Riku Räisänen Avatar answered Sep 23 '22 08:09

Riku Räisänen