堆栈跟踪错误仍然显示在我的控制台上

IT技术 javascript reactjs firebase
2021-05-02 20:07:13

我正在尝试通过 firebase 进行身份验证注册,一切正常。但我正在尝试对不正确/空字段做一些错误处理,我得到:

index.js:1509 Uncaught Error: The error you provided does not contain a stack trace.

我能够从 firebase 检索错误消息,然后将其置于错误状态并将消息呈现在屏幕上,但堆栈跟踪错误仍然显示在我的控制台上。

export const signUp = (newUser) => async (dispatch, getState, { getFirebase, getFirestore }) => {
    try {
        const firebase = getFirebase();
        const firestore = getFirestore();

        const response = await firebase.auth().createUserWithEmailAndPassword(
            newUser.email,
            newUser.password
        );

        firestore.collection('users').doc(response.user.uid).set({
            firstName: newUser.firstName,
            lastName: newUser.lastName,
            initials: newUser.firstName[0] + newUser.lastName[0]
        });

        dispatch({
            type: SIGN_UP_SUCCESS
        });
    } catch(err) {
        dispatch({
            type: SIGN_UP_ERR,
            payload: err
        });
        console.log(err)
    }
};
import React, { useRef } from 'react';
import { Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
import { signUp } from '../../store/actions/authActions';

const SignUp = props => {
    const emailRef = useRef();
    const passwordRef = useRef();
    const firstNameRef = useRef();
    const lastNameRef = useRef();

    const handleSubmit = (e) => {
        e.preventDefault();
        const newUser = {
            email: emailRef.current.value,
            password: passwordRef.current.value,
            firstName: firstNameRef.current.value,
            lastName: lastNameRef.current.value
        };
        props.signUp(newUser);
    };

    if(props.auth.uid) return <Redirect to='/' />

    console.log('props: ', props)
    return (
        <div className="container">
            <form onSubmit={(e) => handleSubmit(e)} className="white">
                <h5 className="grey-text text-darken-3">Sign Up</h5>
                <div className="input-field">
                    <label htmlFor="email">email</label>
                    <input type="email" id="email" ref={emailRef} />
                </div>
                <div className="input-field">
                    <label htmlFor="password">password</label>
                    <input type="password" id="password" ref={passwordRef} />
                </div>
                <div className="input-field">
                    <label htmlFor="firstName">first name</label>
                    <input type="text" id="firstName" ref={firstNameRef} />
                </div>
                <div className="input-field">
                    <label htmlFor="lastName">last name</label>
                    <input type="text" id="lastName" ref={lastNameRef} />
                </div>
                <div className="input-field">
                    <button className="button btn pink lighten-1 z-depth-0">Sign up</button>
                    <div className="red-text center">
                        { props.authErr ? <p>{ props.authErr }</p> : null }
                    </div>
                </div>
            </form>
        </div>
    );
};

const mapStateToProps = state => {
    console.log('auth err:', state.auth.authError)
    return {
        auth: state.firebase.auth,
        authErr: state.auth.authError
    }
};

export default connect(mapStateToProps, { signUp })(SignUp);

不太确定如何处理堆栈跟踪错误

这里的错误图像是我的控制台的图像

2个回答

我认为 await 关键字可能会导致此错误。try..catch 无法捕获 await 语句中的错误。

我认为这个错误是由 firebase 库打印的,然后它返回错误(这是你在 catch 中收到的)。我觉得奇怪的是,如果你使用“then,catch”,错误不会出现,它只在使用 async await 时显示。