Я пытаюсь отобразить данные, которые хранятся как переменная состояния userList
. Я пытаюсь map
каждый объект и отображать параметры name
и email
для каждого объекта, но он ничего не отображает на веб-странице. Я вижу данные только с использованием console.log()
. Я отображаю пользователей, использующих displayUsers()
, и получаю данные из конечной точки API, используя getAllUser()
.
Я думаю, что моя displayUsers()
функция неверна.
Код:
import React, { Component } from 'react';
import axios, { post } from 'axios';
class App extends Component {
constructor(props){
super(props);
this.state = {
userList:[]
}
}
ComponentDidMount(){
if(window.sessionStorage.getItem("ud") !== null){
var _userData = JSON.parse(window.sessionStorage.getItem("ud"));
this.userDetails = _userData;
}
this.getAllUser();
}
getAllUser(){
axios({
method:"GET",
url:"http://62.210.93.54:6010/api/getAllUser",
auth:{
username:this.userDetails.email,
password:this.userDetails.password
}
}).then((response)=>{
console.log(response.data);
this.setState({
userList:response.data.results
})
})
}
displayUsers(){
return this.state.userList.map( user => {
return(
<div className="item-card">
<div className="info">
<div className="username">Username: {user.name}</div>
</div>
<div className="del-wrap">
<img src={require("../../images/cancel.svg")}/>
</div>
</div>
);
})
}
render() {
return(
<div className="users-wrap">
<h1>Users</h1>
<div className="task-content">
<div className="user-wrap">
<div className="users">
<div className="item-card add">
<img src={require("../../images/plus.svg")} className="plus-icon" />
<div className="lbl">Add a new User</div>
</div>
{this.displayUsers()}
</div>
</div>
</div>
</div>
);
}
}
export default App;
Схема модели:
{
"results": [
{
"createdBy": null,
"updatedBy": "Ankit",
"createdDate": 1523892363509,
"updatedDate": 1524066767311,
"id": "5ad4c1964417fc66067b29cf",
"userName": "admin",
"email": "ankit@woocation.com",
"roles": [
"USER"
]
},
{
"createdBy": null,
"updatedBy": null,
"createdDate": 1523971940177,
"updatedDate": 1523971940177,
"id": "5ad5f7640ff4ec580b885a2e",
"userName": "varun",
"email": "varun@woocation.com",
"roles": [
"ADMIN"
]
},
{
"createdBy": null,
"updatedBy": null,
"createdDate": 1524302563169,
"updatedDate": 1524302563169,
"id": "5adb02e30ff4ec53076ffbb7",
"userName": "Rahul",
"email": "rahul@woocation.com",
"roles": [
"admin"
]
},
{
"createdBy": null,
"updatedBy": null,
"createdDate": 1524303894654,
"updatedDate": 1524303894654,
"id": "5adb08160ff4ec53076ffbbb",
"userName": "Nandita",
"email": "nandita@woocation.com",
"roles": [
"member"
]
},
{
"createdBy": null,
"updatedBy": null,
"createdDate": 1524308787960,
"updatedDate": 1524308787960,
"id": "5adb1b330ff4ec53076ffbc2",
"userName": "user",
"email": "user@woocation.com",
"roles": [
"USER"
]
},
{
"createdBy": null,
"updatedBy": null,
"createdDate": 1524327504461,
"updatedDate": 1524327504461,
"id": "5adb64500ff4ec53076ffbc4",
"userName": "Rinku",
"email": "test@woocation.com",
"roles": [
"admin"
]
}
],
"httpStatus": "OK",
"message": "All Users response"
}