Javascript .then和.catch都在同一代码块中执行

Javascript .then和.catch都在同一代码块中执行,javascript,react-native,Javascript,React Native,所以我试图阻止项目进入数组,如果它已经存在一次。基本上,函数正在执行其工作,不允许插入已存在的项。但是,我使用.catch和else输入的所有错误消息都显示在屏幕上,这意味着.then和.catch都在执行中,我不明白为什么。。。 这是我的代码: function onBuy() { const email = firebase.auth().currentUser.email; const ref = firebase.firestore().collecti

所以我试图阻止项目进入数组,如果它已经存在一次。基本上,函数正在执行其工作,不允许插入已存在的项。但是,我使用.catch和else输入的所有错误消息都显示在屏幕上,这意味着.then和.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。您没有指定对的回调。catch;您所做的只是在之后立即调用警报。Alert()。然后

所以你应该

someAsyncOperation(params)
 .then(function(result){
  // Do something with the result
 })
 .catch(function(error){
 // Handle error
});
还请注意,您的所有。然后似乎没有返回任何内容。

找到了解决方案。 刚刚在.catch中的arrow函数中传递了Alert.Alert()。 像这样:


仅使用堆栈片段来显示工作示例。否则,只需将代码块与'code here'一起使用,或者对多行代码使用'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''。无论catch返回什么,都是promise的解析方式(alert返回undefined)。您的.then函数似乎也没有返回任何内容。您必须将回调传递给
catch
。您正在调用
Alert.Alert()
我还将根据调用的
catch
更改消息,以便我知道调用的是哪一个。您有多个类似的警报,这将使跟踪bug成为一个问题。有点相关:您正在调用一个函数,您应该在其中传递函数引用。在本例中,
catch()
setTimeout()
。您使用
then()
正确地完成了操作,因此请遵循相同的模式。谢谢,我对这一点非常陌生,因此它对我非常有帮助:)@Debaryoh
    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'));
}