【问题标题】:Amazon Cognito confirmPassword fails with (TypeError: Converting circular structure to JSON)Amazon Cognito confirmPassword 失败并显示 (TypeError: Converting circular structure to JSON)
【发布时间】:2018-08-31 06:04:49
【问题描述】:

我有一个两步忘记密码的过程。我正在使用 React 渲染两个表单。第一个表单采用电子邮件地址,并在提交后成功执行我的 resetPassword() 函数。该函数通过电子邮件成功地向用户发送了一个安全代码。这部分工作正常。

然后,呈现下一个表单,其中包含安全代码和密码(密码和确认密码 - 显然它们必须相同)。然后,在提交此表单后,它会执行 confirmPassword() 函数。然而,这个函数进入 catch 块并抛出以下异常:

exception:TypeError: Converting circular structure to JSON

由于我是 Node.js 的新手,我不确定这是从哪里来的,或者如何调试它。有什么建议?

我的两个功能如下。同样,这是第二个失败的功能。另外,我在 Cognito 中确认了抛出异常后,用户状态仍然是Enabled / RESET_REQUIRED 状态。

注意:我已将代码标记为 (// ERROR IS HAPPENING HERE) - 这是引发异常的代码部分。请参阅下面代码的最底部。

  resetPassword() {
    const userPool = new CognitoUserPool({
      UserPoolId: config.cognito.USER_POOL_ID,
      ClientId: config.cognito.APP_CLIENT_ID
    });

    // setup cognitoUser first
    let cognitoUser = new CognitoUser({
        Username: this.state.email,
        Pool: userPool
    });

    // initiate the forgotPassword flow, this part sends a security code to the email address given.
    const promise = new Promise((resolve, reject) => {
      cognitoUser.forgotPassword({
        onSuccess: function(result) {
          console.log(util.inspect(result, { showHidden: true, depth: null }));
          // var str = JSON.stringify(result, null, 4);
          // console.log("success, result data: " + str);
          resolve(result);
        },
        onFailure: function(err) {
          console.log(util.inspect(err, { showHidden: true, depth: null }));
          // var str = JSON.stringify(err, null, 4);
          // console.log("error, e data: " + str);
          reject(err);
        },
        undefined
      });
    });
    return promise;
  }

  confirmPassword(username, verificationCode, newPassword) {
    const userPool = new CognitoUserPool({
      UserPoolId: config.cognito.USER_POOL_ID,
      ClientId: config.cognito.APP_CLIENT_ID
    });

    // setup cognitoUser first
    let cognitoUser = new CognitoUser({
        Username: this.state.email,
        Pool: userPool
    });

    const promise = new Promise((resolve, reject) => {
      cognitoUser.confirmPassword(verificationCode, newPassword, {
            onFailure: (err) => {
              console.log("onFailure:" + err);
              reject(err);
            },
            onSuccess: (res) =>  {
              console.log("onSuccess:");
              resolve();
            },
            undefined
        });
    });
    return promise;
  }



handleSubmit = async event => {
    event.preventDefault();

    this.setState({ isLoading: true, emailSubmitted: true });

    try {
      let newUser = await this.resetPassword();
      console.log(util.inspect(newUser, { showHidden: true, depth: null }));
      // var str = JSON.stringify(newUser, null, 4);
      // console.log("newUser:" + str);
    } catch (e) {
      console.log(util.inspect(e, { showHidden: true, depth: null }));
      // var str = JSON.stringify(e, null, 4);
      // console.log("Exception:" + str);
    }

    this.setState({ isLoading: false });
  }

  handleConfirmationSubmit = async event => {
    event.preventDefault();

    this.setState({ isLoading: true });

    try {
      let confirm_result = await     this.confirmPassword(this.state.securityCode, this.state.password, this);
      console.log(util.inspect(confirm_result, { showHidden: true, depth: null }));
      // console.log("confirm_result:" + confirm_result);
    } catch (e) {
      // console.log(util.inspect(e));
      // console.log("exception:" + e);
      // ERROR IS HAPPENING HERE.
      console.log(util.inspect(e, { showHidden: true, depth: null }));
      console.log(new Error().stack);
      /* Stack trace shows:
      Error
        at ForgotPassword._callee2$ (ForgotPassword.js:154)
        at tryCatch (runtime.js:62)
        at Generator.invoke [as _invoke] (runtime.js:296)
        at Generator.prototype.(anonymous function) [as throw] (http://localhost:3000/static/js/bundle.js:61658:21)
        at step (ForgotPassword.css?776b:26)
        at ForgotPassword.css?776b:26
      */
      this.setState({ isLoading: false });
    }
  }

【问题讨论】:

    标签: node.js reactjs amazon-web-services aws-sdk amazon-cognito


    【解决方案1】:

    我记得circular JSON error 发生在我试图在两个不同的上下文中修改 Cognito 用户数据时,而底层数据集是相同的。在我的用例中,我在浏览器和 lambda 函数上修改了一次数据集。我不记得 Cognito 给了我一个直观的错误信息来解决这个问题。当我开始在浏览器与 lambda 回调中对不同的数据集进行操作时,这种情况就消失了。也许您可以尝试如下操作,看看是否有效 -

    const userPool = new CognitoUserPool({
      UserPoolId: config.cognito.USER_POOL_ID,
      ClientId: config.cognito.APP_CLIENT_ID
    });
    
    // setup cognitoUser first
    let cognitoUser = new CognitoUser({
        Username: this.state.email,
        Pool: userPool
    });
    
     resetPassword() {
        // initiate the forgotPassword flow, this part sends a security code to the email address given.
        const promise = new Promise((resolve, reject) => {
          cognitoUser.forgotPassword({
            onSuccess: function(result) {
              console.log(util.inspect(result, { showHidden: true, depth: null }));
              // var str = JSON.stringify(result, null, 4);
              // console.log("success, result data: " + str);
              resolve(result);
            },
            onFailure: function(err) {
              console.log(util.inspect(err, { showHidden: true, depth: null }));
              // var str = JSON.stringify(err, null, 4);
              // console.log("error, e data: " + str);
              reject(err);
            },
            undefined
          });
        });
        return promise;
      }
    
      confirmPassword(username, verificationCode, newPassword) {
        const promise = new Promise((resolve, reject) => {
          cognitoUser.confirmPassword(verificationCode, newPassword, {
                onFailure: (err) => {
                  console.log("onFailure:" + err);
                  reject(err);
                },
                onSuccess: (res) =>  {
                  console.log("onSuccess:");
                  resolve();
                },
                undefined
            });
        });
        return promise;
      }
    

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 1970-01-01
      • 2018-11-19
      • 2023-01-18
      • 2019-04-03
      • 1970-01-01
      • 2017-08-13
      • 2015-08-14
      • 2022-12-26
      相关资源
      最近更新 更多