首页 文章

反应Cognito用户池 - 客户端尝试写入未经授权的属性

提问于
浏览
0

我正在试图弄清楚这个问题 . 我在我的反应应用程序中有以下代码 . 我需要为用户添加一些添加单元/ apt号的方法,所以我添加了一个自定义属性 .

一切正常,但当我包含 apt_number: this.state.unitNumber, 时,我收到错误 {code: "NotAuthorizedException", name: "NotAuthorizedException", message: "A client attempted to write unauthorized attribute"} .

我确实进入了我的设置并使属性可写(我尝试使用 Unitapt number 属性)

这是我的代码:

const receivedNewUser = await Auth.signUp({
  username: this.state.email,
  password: this.state.password,
  attributes: {
    phone_number: this.state.phone,
    address: this.state.streetAddress,
    birthdate: this.state.dob,
    locale: this.state.zipCode,
    given_name: this.state.fname,
    family_name: this.state.lname,
    apt_number: this.state.unitNumber,
  },
});

到底是怎么回事?

enter image description here

1 回答

  • 1

    您需要添加 custom: 作为属性名称的前缀 .

    您的代码应为:

    const receivedNewUser = await Auth.signUp({
      username: this.state.email,
      password: this.state.password,
      attributes: {
        phone_number: this.state.phone,
        address: this.state.streetAddress,
        birthdate: this.state.dob,
        locale: this.state.zipCode,
        given_name: this.state.fname,
        family_name: this.state.lname,
        'custom:apt_number': this.state.unitNumber,
      },
    });
    

相关问题