Amazon Cognito ConfirmPassword失败,并显示(TypeError:将循环结构转换为JSON) [英] Amazon Cognito confirmPassword fails with (TypeError: Converting circular structure to JSON)

查看:85
本文介绍了Amazon Cognito ConfirmPassword失败,并显示(TypeError:将循环结构转换为JSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a two step forgot password process. I'm rendering two forms using React. The first form takes an email address and upon submit, successfully executes my resetPassword() function. The function successfully sends the user a security code via email. This part works fine.

然后,呈现下一个表单,其中包含安全代码和密码(密码和ConfirmPassword-显然它们必须相同)。然后,在提交此表单后,它将执行confirmPassword()函数。但是,此函数进入catch块并引发以下异常:

Then, the next form is rendered which takes in the security code and a password (password and confirmPassword - they must be the same, obviously). Then, upon submit of this form, it executes the confirmPassword() function. This function however enters the catch block and throws the following exception:

exception:TypeError: Converting circular structure to JSON

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

As I'm very new to Node.js, I'm not sure where this is coming from, or how to debug it. Any suggestions?

下面是我的两个函数。同样,这是第二个失败的功能。另外,在引发异常后,我在Cognito中进行了确认,并且用户状态仍然为 Enabled / RESET_REQUIRED 状态。

My two functions are below. Again, it's the second function that is failing. Also, I confirmed in Cognito after the exception is thrown, and the user status is still in Enabled / RESET_REQUIRED status.

注意:我已经用(//此处发生错误)标记了代码-这是引发异常的代码部分。参见下面的代码底部。

Note: I've marked the code with (// ERROR IS HAPPENING HERE) - this is the section of code where the exception is getting thrown. See the very bottom of the code below.

  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 });
    }
  }


推荐答案

I还记得当我尝试在两个不同的上下文中修改Cognito用户数据而基础数据集相同时发生的循环JSON错误。在我的用例中,我在浏览器和lambda函数上都修改过一次数据集。我不记得Cognito会给我返回直观的错误消息来解决此问题。当我开始在浏览器和lambda回调中对不同的数据集进行操作时,这种情况就消失了。也许您可以尝试执行以下操作,看看是否可行-

I remember this circular JSON error happening when I was trying to modify the Cognito User data across two different contexts while the underlying dataset is the same. In my use case, I was modifying the dataset once at the browser and also at the lambda function. I don't remember Cognito giving me back an intuitive error message to solve this. When I started operating on different data sets at browser vs. lambda callback, this went away. Maybe you could try doing as below and see if that works -

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;
  }

这篇关于Amazon Cognito ConfirmPassword失败,并显示(TypeError:将循环结构转换为JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆