【发布时间】: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