reactjs - aws amplify auth 自定义注册无法映射未定义
问题描述
学习使用 aws amplify 并且我想按照我找到的示例覆盖他们的注册组件,但我不断收到下面的未定义错误。cognito 用户池要求用户使用电子邮件作为用户名和名称属性进行注册。
这是我的注册组件。
import React from "react";
import { SignUp } from "aws-amplify-react";
export class CustomSignUp extends SignUp {
constructor(props) {
super(props);
this._validAuthStates = ["signUp"];
this.state = {
userName: 'test',
password: 'MyTest123!',
email: 'test@gmail.com',
phoneNumber: '1231231234'
};
}
handleSubmit = async (event) => {
event.preventDefault();
console.log(this.state);
const response = await super.signUp({
username: this.state.email,
password: this.state.password,
attributes: {
email: this.state.email,
phone_number: this.state.phone,
name: this.state.userName
}
});
console.log(response);
}
showComponent(theme) {
return (
<form onSubmit={this.handleSubmit}>
<input
type="text"
value={this.state.userName}
onChange={event => this.setState({ userName: event.target.value })}
placeholder="Enter your username"
required
/>
<input
type="text"
value={this.state.password}
onChange={event => this.setState({ password: event.target.value })}
placeholder="Enter your password"
required
/>
<input
type="text"
value={this.state.email}
onChange={event => this.setState({ email: event.target.value })}
placeholder="Enter your email"
required
/>
<button>Sign up</button>
</form>
);
}
}
错误:
Uncaught (in promise) TypeError: Cannot read property 'map' of undefined
at CustomSignUp.SignUp.validate (SignUp.js:94)
at CustomSignUp.SignUp.signUp (SignUp.js:203)
解决方案
我有同样的问题。this.signupFields
直接在 CustomSignUp 类的构造函数中声明似乎是我可以修复它的唯一方法。似乎这并没有从父类的道具中传递下来。
export class CustomSignUp extends SignUp {
constructor(props) {
super(props);
this._validAuthStates = ["signUp"];
this.state = {
userName: 'test',
password: 'MyTest123!',
email: 'test@gmail.com',
phoneNumber: '1231231234'
};
this.signUpFields = [
{
label: 'Username',
key: 'username',
required: true,
displayOrder: 1,
type: 'string'
},
{
label: 'Email',
key: 'email',
required: true,
displayOrder: 2,
type: 'string'
},
{
label: 'Password',
key: 'password',
required: true,
displayOrder: 3,
type: 'password'
},
{
label: 'Phone Number',
key: 'phone_number',
required: false,
displayOrder: 4,
type: 'string'
}
];
}
我不确定是否需要电话号码,但我注意到 defaultSignUpFields 变量已设置为 required=true,因此我故意在此处覆盖它。
推荐阅读
- c - 在 640x480 16 色 VGA 模式下无法绘制像素
- javascript - JavaScript:在同一个循环中迭代推送的元素
- json - 在 web api 中以 json 格式显示 sql 查询结果
- node.js - 为 Nodejs 项目设置 Azure 函数环境变量
- spring-boot - 使用 oauth 和带有表单登录的 UI 的 Secure Rest API
- javascript - 为什么我在chartjs上的数据不是从它应该的数据开始
- vue.js - 无法使用 v-bind:to 指令使 `/${i18n.locale}` 工作
- react-native - 如何区分codepush和storepush
- javascript - 如何在 Odoo 10 中为特定模型禁用看板视图中的拖放?
- c# - 如何将带有cshtml文件的类库引用到azure函数