gpt4 book ai didi

node.js - Amazon Cognito 确认密码失败并显示 (TypeError : Converting circular structure to JSON)

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:32 26 4
gpt4 key购买 nike

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

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

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

最佳答案

我记得当我尝试跨两个不同上下文修改 Cognito 用户数据而底层数据集相同时,发生了这个循环 JSON 错误。在我的用例中,我在浏览器和 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;
}

关于node.js - Amazon Cognito 确认密码失败并显示 (TypeError : Converting circular structure to JSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49417859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com