尝试使用令牌验证用户,但emailAddressVerificationToken未定义

时间:2019-01-13 07:50:04

标签: javascript reactjs graphql apollo

我正在尝试通过sendgrid单击电子邮件链接来验证用户。您可以在下面查看要尝试传递values.emailAddressVerificationToken的代码,以便将其传递到我的操作文件中,但是我在verify组件中未定义emailAddressVerificationToken。

enter image description here

验证组件

  import { verifyEmailSubmit } from "../../stores/Auth/actions";
    // gets the token in the URL
    getVerifiedEmailToken = () => {
    const query = parse(this.props.location.search);
    return query.token;
  };

     componentDidMount(values) {
        // this means the user has clicked the link, so we'll verify the email here
        const { verifyEmail } = this.props;
        console.log(verifyEmail);
        console.log(values);
        values.emailAddressVerificationToken = this.getVerifiedEmailToken();
        verifyEmail(values);
      }

    const mapDispatchToProps = dispatch => ({
      verifyEmail: params => dispatch(verifyEmailSubmit(params))
    });

    export default withRouter(
      connect(
        mapStateToProps,
        mapDispatchToProps
      )(VerifyEmailComplete)
    );

身份验证操作文件

    export function verifyEmailSubmit(params) {
      const mutation = `
      mutation {
        verifyEmail(
          emailAddressVerificationToken: "${params.emailAddressVerificationToken}"
        )
      }      
    `;

      return dispatch => {
        dispatch(authInProgress());

        return ApolloService.mutate({ mutation })
          .then(response => {
            const { verifyEmail } = response.data;

            dispatch(resetAppMessages());
            dispatch(verifyEmailSuccess());
            return verifyEmail;
          })
          .catch(error => {
            dispatch(verifyEmailFailure());
            // Need to throw this error so that the callee (react component) can use this error in it's own catch block
            throw error;
          });
      };
    }

1 个答案:

答案 0 :(得分:0)

此错误是由于componentDidMount中的这一行引起的

values.emailAddressVerificationToken = this.getVerifiedEmailToken();
// values is undefined. react don't send any argument

componentDidMount不发送任何参数,并且您正在接受参数,因为values的值未定义。请参阅here