Reputation: 1953
I am trying to display data which is stored as userList
state variable. I am trying to map
each object and display name
and email
parameter from each object but it does not display anything on web page I can only see the data using console.log()
. I am displaying Users using displayUsers()
and getting data from API endpoint using getAllUser()
.
I think my displayUsers()
function is wrong.
Code:
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;
Model Schema:
{
"results": [
{
"createdBy": null,
"updatedBy": "Ankit",
"createdDate": 1523892363509,
"updatedDate": 1524066767311,
"id": "5ad4c1964417fc66067b29cf",
"userName": "admin",
"email": "[email protected]",
"roles": [
"USER"
]
},
{
"createdBy": null,
"updatedBy": null,
"createdDate": 1523971940177,
"updatedDate": 1523971940177,
"id": "5ad5f7640ff4ec580b885a2e",
"userName": "varun",
"email": "[email protected]",
"roles": [
"ADMIN"
]
},
{
"createdBy": null,
"updatedBy": null,
"createdDate": 1524302563169,
"updatedDate": 1524302563169,
"id": "5adb02e30ff4ec53076ffbb7",
"userName": "Rahul",
"email": "[email protected]",
"roles": [
"admin"
]
},
{
"createdBy": null,
"updatedBy": null,
"createdDate": 1524303894654,
"updatedDate": 1524303894654,
"id": "5adb08160ff4ec53076ffbbb",
"userName": "Nandita",
"email": "[email protected]",
"roles": [
"member"
]
},
{
"createdBy": null,
"updatedBy": null,
"createdDate": 1524308787960,
"updatedDate": 1524308787960,
"id": "5adb1b330ff4ec53076ffbc2",
"userName": "user",
"email": "[email protected]",
"roles": [
"USER"
]
},
{
"createdBy": null,
"updatedBy": null,
"createdDate": 1524327504461,
"updatedDate": 1524327504461,
"id": "5adb64500ff4ec53076ffbc4",
"userName": "Rinku",
"email": "[email protected]",
"roles": [
"admin"
]
}
],
"httpStatus": "OK",
"message": "All Users response"
}
Upvotes: 0
Views: 7157
Reputation: 31909
Before I edited your title, this would be a handy shortcut to display formatted JSON data right in the document.
displayUsers = () => <pre>{JSON.stringify(this.state.userList, null, ' ')}</pre>
to only display userName
and email
, use a whitelist array like this
JSON.stringify(this.state.userList, ["userName", "email"], ' ')
You can read more about JSON.stringify(value[, replacer[, space]])
here.
Upvotes: 1
Reputation: 281626
As per your model schema, your user object contains userName
and not name
, so you would write user.userName
in your displayUsers
method, Also a key param is helpful in performance optimisation and you should add a unique key on the element returned by the iterator
displayUsers(){
return this.state.userList.map( user => {
return(
<div className="item-card" key={user.id}>
<div className="info">
<div className="username">Username: {user.userName}</div>
</div>
<div className="del-wrap">
<img src={require("../../images/cancel.svg")}/>
</div>
</div>
);
})
}
Upvotes: 1
Reputation: 1394
You need to add a key to the root element of map object to be returned. `
displayUsers(){
return this.state.userList.map( user => {
return(
<div key={user.name} 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>
);
})
}
Upvotes: 1