React-Native中能否更新AWS Cognito用户邮箱属性?遇参数异常报错
React-Native中更新AWS Cognito用户邮箱属性的问题解决
问题描述
在React-Native中尝试更新AWS Cognito用户的邮箱属性,调用updateUserAttributes方法时触发InvalidParameterException错误,提示属性不存在于schema中。
用户实现代码:
import {Auth} from 'aws-amplify'; let user = await Auth.currentAuthenticatedUser(); user.session = user.signInUserSession; let userAttributes = await Auth.userAttributes(user); let new = { Name: 'email', Value: emailAddress } Auth.updateUserAttributes(user, new).then((res) => { console.log("success", res); }).catch(e => { console.log("EEE", e); });
错误信息:
{"code": "InvalidParameterException", "message": "user.Value: Attribute does not exist in the schema. user.Name: Attribute does not exist in the schema. ", "name": "InvalidParameterException"}
解决方法
1. 修正updateUserAttributes参数格式
updateUserAttributes的第二个参数要求是键值对对象,直接以属性名为键、属性值为值,而非带Name和Value字段的结构。修改后的代码:
import {Auth} from 'aws-amplify'; try { const user = await Auth.currentAuthenticatedUser(); // 无需手动赋值session,currentAuthenticatedUser返回的对象已包含必要认证信息 const result = await Auth.updateUserAttributes(user, { email: emailAddress // 直接用属性名作为键 }); console.log("更新成功", result); } catch (e) { console.log("更新失败", e); }
2. 确认Cognito用户池属性配置
登录AWS控制台,进入目标Cognito用户池:
- 进入属性标签页,确认
email属性已添加到用户池属性列表 - 检查属性设置,确认
email属性的"可修改"权限已开启(允许用户自行修改该属性)
3. 移除冗余代码
原代码中user.session = user.signInUserSession和userAttributes的获取操作完全多余,Auth.currentAuthenticatedUser()返回的用户对象已包含更新属性所需的全部认证信息,可直接使用。
内容的提问来源于stack exchange,提问作者Christian Rainer Sassmann
相关产品推荐
相关产品推荐

