所以我有一个函数可以返回json用户列表。 我想逐个字符串打印列表,例如: 用户1 用户2 用户3 但是输出什么都没有。 我刚刚开始使用react,有什么帮助吗?
export default function Users() {
const classes = useStyles()
const [UsersRow, setUsersRow] = React.useState()
React.useEffect(() => {
fetch("http://localhost:5000/users", {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "pass " + window.localStorage.getItem("access_token"),
},
})
.then(resp => {
return resp.json()
})
.then(data => {
setUsersRow(data)
})
.catch(error => {
console.log(error)
window.localStorage.removeItem("access_token")
window.location.replace("/")
})
}, [])
if (UsersRow === undefined) {
return <div />
}
return (
<div className={classes.root}>
<h1>Users</h1>
<Table className={classes.table} size="small">
<TableBody>
{UsersRow.map(row => (
<TableRow>
<TableCell align="left">{row.user}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
)
}
后端函数返回这样的json:
[
"User1",
"User2",
"User3"
]
后端功能:
@app.route('/users', methods=['GET'])
@jwt_required
def user_list():
list_1 = []
users = ldap.get_group_members('ship_crew')
for user in users:
list_1.append(str(user).split(",")[0].split("=")[1].encode())
return jsonify(list_1)
答案 0 :(得分:0)
如果后端返回以下内容:
[
"User1",
"User2",
"User3"
]
然后,您将这样渲染:
<TableBody>
{UsersRow.map(user => (
<TableRow>
<TableCell align="left">{user}</TableCell>
</TableRow>
))}
</TableBody>
user
上没有row
属性。