AWS Amplify 注册 InvalidParameterException:属性不符合

问题描述 投票:0回答:1

我有一个 React Native 应用程序,我正在尝试将 AWS amplify 集成到我的项目中。现在,我已经收集了创建帐户所需的所有必需信息。我把一切都保存在状态中。当我尝试实际注册并创建帐户时,我遇到了一个问题,我已尝试多次解决但似乎没有任何效果。

这是我的 sugnupUser 函数:

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

这里是useState中存储的信息:

  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('')

不确定为什么我不断收到此错误或如何修复该错误。

javascript reactjs amazon-web-services react-native amazon-cognito
1个回答
0
投票

文档说您需要将用户属性放入

options
对象中:

import { signUp } from 'aws-amplify/auth';

async function handleSignUp() {
  try {
    await signUp({
      username: 'jdoe',
      password: 'mysecurerandompassword#123',
      options: {
        userAttributes: {
          email: '[email protected]',
          phone_number: '+12128601234', // E.164 number convention
          given_name: 'Jane',
          family_name: 'Doe',
          nickname: 'Jane'
        }
      }
    });
  } catch (e) {
    console.log(e);
  }
}

https://docs.amplify.aws/javascript/build-a-backend/auth/manage-user-profile/

© www.soinside.com 2019 - 2024. All rights reserved.