【问题标题】:Forget password is working fine, but in change password, error: Cannot read property 'value' of undefined. Password value can not read忘记密码工作正常,但在更改密码时,错误:无法读取未定义的属性“值”。密码值无法读取
【发布时间】:2020-04-03 05:06:41
【问题描述】:

更改密码.ts

changePw(email: HTMLInputElement, contactNo: HTMLInputElement, username: HTMLInputElement, password: HTMLInputElement) {
  console.log("changepasswordhere");
  const user = {
    password: password.value,
    attributes: {
      email: email.value
    }
  }
  console.log(user)

  Auth.currentAuthenticatedUser()
    .then(user => {
      console.log("data " + user);
      this.toVerifyEmail = true;
    })
    .catch(err => console.log(err));
  // console.log(user)
}

更改密码.html

<label for="inputEmail" class="sr-only">Username</label>
<!-- <input type="text" required (keypress)="numberOnly($event)" id="confirmationcode" class="form-control" placeholder="Confirmation Code"  [(ngModel)]="confirmationcode" name="confirmationform" required autofocus> -->
<input
  type="number"
  class="form-control"
  id="exampleInputverifycode1"
  aria-describedby="verifyCodeHelp"
  placeholder="Enter the code"
  #verifycode
/>
<input
  type="password"
  id="inputPassword"
  class="form-control"
  placeholder="New Password"
  [(ngModel)]="newpassword"
  name="loginform"
  required
  autofocus
/>
<input
  type="password"
  id="inputPassword"
  class="form-control"
  placeholder="Confirm New Password"
  [(ngModel)]="confirmpassword"
  name="loginform"
  required
  autofocus
/>
<div class="alert alert-danger" *ngIf="message" role="alert">
  {{message}}
</div>

<button
  class="btn btn-lg btn-primary btn-block"
  (click)="changePw(email,password,username,contactNo)"
>
  Submit
</button>

在忘记密码中,输入用户名,cognito会检查名称是否存在,然后发送OTP。之后更改密码用户必须输入 OTP 来验证、新密码并确认新密码。由于我是 aws-amplify 的新手,请任何人都可以在这里给我建议。

【问题讨论】:

    标签: angular aws-amplify


    【解决方案1】:

    在您的 HTML 中,您通过 [(ngModel] 将两个输入字段绑定到密码变量:

    <input
      type="password"
      id="inputPassword"
      class="form-control"
      placeholder="New Password"
      [(ngModel)]="newpassword"
      name="loginform"
      required
      autofocus
    />
    <input
      type="password"
      id="inputPassword"
      class="form-control"
      placeholder="Confirm New Password"
      [(ngModel)]="confirmpassword"
      name="loginform"
      required
      autofocus
    />
    

    因此,在您的打字稿文件中,您应该有两个变量:newpassword 和 confirmpassword。这是 ngModel 将与之交互的内容,以使它们更新到用户在输入字段中输入的任何内容。

    最后,如果你的 typescript 中已经有这两个,你可以像这样在你的 changePw 函数中使用它:

    changePw(email: HTMLInputElement, contactNo: HTMLInputElement, username: HTMLInputElement, password: HTMLInputElement) {
      console.log("changepasswordhere");
      const user = {
        // edited here: use your already existing variable
        password: newpassword,
        attributes: {
          email: email.value
        }
      }
      console.log(user)
    
      Auth.currentAuthenticatedUser()
        .then(user => {
          console.log("data " + user);
          this.toVerifyEmail = true;
        })
        .catch(err => console.log(err));
      // console.log(user)
    }
    

    您可能想检查新密码和确认密码是否相等,但这是您可以采用的一种方法。

    【讨论】:

    • 嗨,gerstam,感谢您的回复。但是我想问一下如何使用密码属性下的那些变量确认新密码和新密码?喜欢password: newpassword, password: confirmpassword
    • changePw(email: HTMLInputElement, contactNo: HTMLInputElement, username: HTMLInputElement, newpassword: HTMLInputElement,confirmnewpassword: HTMLInputElement) { console.log("changepasswordhere"); const user = { password: newpassword,confirmnewpassword, attributes: { email: email.value } } console.log(user.password)
    • 那么请更新您的问题 ;-) 这也可以在 cmets 压缩代码时保持代码清洁
    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2021-09-16
    • 2020-08-16
    • 2014-07-09
    相关资源
    最近更新 更多