so I try to prevent from item to get into an array if it's already there once. Basically, the function is doing its job and not allowing to insert an item that already exists. But, all the error messages that I put using .catch and else are being shown on the screen, meaning that both .then and .catch are being executed and I don't understand why... This is my code:
function onBuy() {
const email = firebase.auth().currentUser.email;
const ref = firebase.firestore().collection('parties').doc(String(name)).collection('users').doc(String(email));
const ref2 = firebase.firestore().collection('users').doc(String(email));
ref.get()
.then((doc) => {
if (!doc.exists) {
ref2.get()
.then((doc2) => {
if (doc2.exists) {
ref.set({
firstname: doc2.data().firstname,
lastname: doc2.data().lastname,
phone: doc2.data().phone
}).then(
ref2.update({ parties: firebase.firestore.FieldValue.arrayUnion(name) }).then(
Actions.pop()))
.catch(Alert.alert('fail'));
}
}).catch(Alert.alert('fail'));
}
else {
Alert.alert('this user already bought a ticket');
}
})
.catch(Alert.alert('this user already bought a ticket'));
}
I tried to search for solutions but didn't found an answer. Thanks in advance :)