【问题标题】:Stack trace error is still showing up on my console堆栈跟踪错误仍然显示在我的控制台上
【发布时间】:2019-12-18 04:01:46
【问题描述】:

我正在尝试通过 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);

不太清楚如何处理堆栈跟踪错误

image of error 这是我的控制台的图像

【问题讨论】:

  • 什么是 index.js 第 1509 行?您能够呈现的错误消息是什么?为什么不将 await 与 set() 方法调用一起使用?
  • 想通了,我的 try catch 不会从等待中捕获错误!您能否详细说明将 await 与 set() 一起使用?非常感谢

标签: javascript reactjs firebase


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我认为你是对的!这是 await 语句中未捕获的错误。关于我应该如何处理它的任何建议?
    • 这对我也有帮助,从使用 await 的 try catch 重构为 .then().catch() 为我解决了这个问题
    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2016-01-11
    • 2017-10-24
    • 2015-06-07
    • 2011-12-04
    相关资源
    最近更新 更多