I am not getting data from backend for my method GET byId. Other methods as getAll(), for example, are working. So i think the problem in code/syntax of Component.
My Component:
class Account extends Component {
accountItem = {
id: '',
name: '',
email: '',
password: '',
link: ''
};
constructor(props) {
super(props);
this.state = {
item: this.accountItem
};
this.reloadAccount = this.reloadAccount.bind(this);
}
componentDidMount() {
this.reloadAccount();
}
reloadAccount(id) {
ApiService.fetchAccountById(id)
.then((res) => {
let account = res.data;
this.setState({
id: account.id,
name: account.name,
email: account.email,
password: account.password,
link: account.link
})
})
}
render() {
const {item} = this.state;
return (
<div>
<div className={c.account}>
<ul>
<li>Id: {item.id}</li>
<li>Name: {item.name}</li>
<li>Email: {item.email}</li>
<li>Password: {item.password}</li>
<li>Link: {item.link}</li>
</ul>
</div>
</div>
)
}
}
ApiService:
fetchAccountById(id) {
return axios.get(ACCOUNT_API_BASE_URL + '/' + id);
}
My backend api: "/api/accounts/{id}" I am getting:
"http://localhost:8080/api/accounts/undefined 400"
What problem could be? I have deleted 'result' from res.data.result, but it still not works(for getAll method worked for me)
When you are calling this.reloadAccount() in componentDidMount(), you are not passing the id due to which the id in fetchAccountById is undefined and thus you are not able to fetch the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With