我想读取用户信息,并将自定义属性等于1的“ Admin”变量设置为yes。 问题是,当我想读取用户属性时,程序因TypeError崩溃:无法读取未定义的属性“属性”
这是我的代码:
import React,{ useState,useEffect } from "react";
import './App.css';
import { Link,withRouter } from "react-router-dom";
import { Nav,Navbar,NavItem,Image } from "react-bootstrap";
import { LinkContainer } from "react-router-bootstrap";
import Routes from "./Routes";
import { Auth } from "aws-amplify";
import logo from './img/download.svg';
function App(props) {
const [isAuthenticating,setIsAuthenticating] = useState(true);
const [isAuthenticated,userHasAuthenticated] = useState(false);
const [userData,setUserData] = useState([]);
const [isAdmin,setIsAdmin] = useState(true);
useEffect(() => {
onLoad();
},[],);
async function handleLogout() {
await Auth.signOut();
userHasAuthenticated(false);
setIsAdmin(false);
props.history.push("/login");
}
async function onLoad() {
try {
await Auth.currentSession();
userHasAuthenticated(true);
await Auth.currentUserInfo().then(user => setUserData(user));
if (userData.attributes['custom:isAdmin'] === "1" ) {
setIsAdmin(true)
}
}
catch(e) {
if (e !== 'No current user') {
alert(e);
}
}
setIsAuthenticating(false);
}
return (...)