React Native集成AWS Amplify注册报错InvalidParameterException求助
AWS Amplify注册触发InvalidParameterException问题解决
问题描述
在React Native应用集成AWS Amplify时,已将注册所需信息存入state,但调用注册函数时触发InvalidParameterException,提示缺少name、preferred_username、email、phone_number必填属性。
相关代码与错误信息
注册函数代码
import { signUp } from 'aws-amplify/auth' const signupUser = () => { signUp({ username: username, password: password, attributes: { username: username, email: email, preferred_username: username, phone_number: `+1${phone}`, 'custom:following': 0, 'custom:followers': 0, 'custom:favorites': 0 } }) .then((response) => { console.log('new user') }) .catch((error) => { console.log('error: ', error) }) }
错误日志
LOG error: [InvalidParameterException: Attributes did not conform to the schema: name: The attribute is required preferred_username: The attribute is required email: The attribute is required phone_number: The attribute is required
State定义
const [username, setUsername] = useState('') const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [verify, setVerify] = useState('') const [name, setName] = useState('') const [phone, setPhone] = useState('') const [location, setLocation] = useState('')
问题原因
- 缺失必填字段:错误提示明确指出缺少
name属性,但当前attributes中未传入该字段。 - 冗余字段传递:
username属于signUp顶层参数,无需在attributes中重复传递。 - 字段值有效性存疑:
phone变量可能未正确赋值,或phone_number格式不符合Cognito要求的E.164标准。
解决方案
修改signUp调用参数,补全必填字段并修正冗余项:
const signupUser = () => { signUp({ username: username, password: password, attributes: { name: name, // 补全缺失的name必填字段 email: email, preferred_username: username, phone_number: `+1${phone}`, // 确保phone为纯数字,格式如1234567890 'custom:following': 0, 'custom:followers': 0, 'custom:favorites': 0 } }) .then((response) => { console.log('new user') }) .catch((error) => { console.log('error: ', error) }) }
额外检查项
- 登录AWS控制台,确认Cognito用户池的属性配置中,
name、preferred_username等字段确实设置为必填;若不需要,可修改为非必填。 - 检查表单输入与state的绑定逻辑,确保
name、email、phone等变量已正确获取用户输入值,而非空字符串。 phone_number必须严格遵循E.164格式,不能包含空格、括号或其他符号,示例:+12065551234。
内容的提问来源于stack exchange,提问作者ojandali
相关产品推荐
相关产品推荐

