I am making a page where if there is something pulled from the database it displays this information, otherwise displays something else.
The data is pulled from a express back end at the moment.
Below is the code.
class ManageWorkstations extends React.Component {
constructor() {
super();
this.state = { AccountDetails: [], exist: "" };
this.getItems = this.getItems.bind(this);
}
// sets the questions from sql into state for questions
getItems() {
try {
var user = window.localStorage.getItem("User");
if (user) {
fetch(`/profile-work-station-detailss/${user}`)
.then(recordset => recordset.json())
.then(results => {
console.log(this.state.AccountDetails);
this.setState({ AccountDetails: results.recordset });
console.log(this.state.AccountDetails);
});
} else {
alert("user not set");
}
} catch (e) {
console.log(e)
}
}
//when the component mounts make the sql questions the
componentDidMount() {
this.getItems()
console.log(this.state.AccountDetails)
}
render() {
if (this.state.AccountDetails.DeskLocation) {
return (
<ul>
<Link to="/profile">
<button style={{ float: "left" }} className="btn btn-secondary">
Account Details
</button>
</Link>
<button
style={{ float: "left" }}
className="btn btn-secondary"
disabled
>
Manage Workstations
</button>
<DisplayAddWorkstation />
<br />
<br />
{this.state.AccountDetails &&
this.state.AccountDetails.map(function (AccountDetails, index) {
return (
<WorkStations AccountDetails={AccountDetails}></WorkStations>
);
})}
</ul>
)
} else {
return (<>this is what I want</>)
}
}
}
export default ManageWorkstations;
As I hope you can see, I am using an if statement in the render to display if these exist. However, the only statement I can get to work with it, is if I set it so that account details do not exist. How would I update this so that it can correctly find this information and not just so that it always thinks it does not exist.
DeskLocationproperty exists onAccountDetailsobject?console.log(this.state.AccountDetails);