因此,如果它已经存在,我会尽力防止项目进入数组。基本上,该功能正在完成其工作,不允许插入已经存在的项目。但是,我使用.CATCH的所有错误消息都在屏幕上显示,表示。这是我的代码:
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'));
}
我试图搜索解决方案,但没有找到答案。预先感谢:)
您必须意识到。您没有将回调指定为 .catch ;您要做的就是在> .wathen 之后立即调用 alter.alert()。
所以你应该有
someAsyncOperation(params)
.then(function(result){
// Do something with the result
})
.catch(function(error){
// Handle error
});
还请注意,您的所有。 找到了解决方案。刚刚通过.catch中的箭头函数传递了alert.alert()。喜欢: 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('error', 'oops, something went wrong'));
}
}).catch(() => Alert.alert('error', 'Sorry, something went wrong'));
}
else {
Alert.alert('Purches fails', 'You have already bought a ticket to this party!');
}
})
.catch(() => Alert.alert('fails', 'user problem'));
}