为了账号安全,请及时绑定邮箱和手机立即绑定

reactjs 使用来自 cognito 的参数获取

reactjs 使用来自 cognito 的参数获取

蛊毒传说 2023-02-17 15:58:49
我正在尝试获取当前登录的用户电子邮件并在我的 fetch() 调用中使用它。我目前可以从 getfirstapi() 获取电子邮件并在我的表单中使用它,但是我无法将它传递到我的 getSecondApi() 中,其中 fetch() 是?我尝试创建一个 getEmail 函数来返回它,但没有成功。形式编辑>   getEmailApi() {    return> Auth.currentAuthenticatedUser().then((user) => {>       const { attributes = {} } = user;>       console.log(attributes['email']);>     })}iv 创建此功能以获取电子邮件但不知道如何将其传递到 getSecondApi
查看完整描述

1 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

我的解决方案


getEmailApi() {

       return Auth.currentAuthenticatedUser().then((user) => {

          const { attributes = {} } = user;

          console.log(attributes['email']);

          let email =  attributes['email']

          return email

        })}

    

     getFirstApi() {

       return Auth.currentAuthenticatedUser().then((user) => {

          this.setState({email: user.attributes.email, formemail: user.attributes.email})

        });

     }

    

      getSecondApi(email) {

        fetch(`https://ezha2ns0bl.execute-api.ap-southeast-2.amazonaws.com/prod/userdata?userEmail=${encodeURIComponent(email)}`)

          .then(res => res.json())

          .then(

            console.log("THIS IS RESULT2 " + email),

            (result) => {

              this.setState({

                firstname: result.Item.userFirstName,

                middlename: result.Item.userMiddleName,

                surname: result.Item.userLastName,

                city: result.Item.userCity,

                postcode: result.Item.userPostcode,

                state: result.Item.userState,

                about: result.Item.userAbout,

    

                formfirstname: result.Item.userFirstName,

                formmiddlename: result.Item.userMiddleName,

                formsurname: result.Item.userLastName,

                formcity: result.Item.userCity,

                formpostcode: result.postcode,

                formstate: result.Item.userState,

                formabout: result.Item.userAbout,

                

              });

              console.log("THIS IS RESULT1 " + result)} ,

          )

      }

    

      BeforDidMount() { 

       this.getEmailApi().then(email => this.getSecondApi(email)); }

    

      componentDidMount() {

        this.BeforDidMount();

        this.getFirstApi();  

      }


查看完整回答
反对 回复 2023-02-17
  • 1 回答
  • 0 关注
  • 104 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号