Я использую реакцию с фоном экспресс-узла.Я пытаюсь получить данные от API узла в переднем конце реакции.API узла возвращает данные успешно, протестировано на почтальоне, но когда я пытаюсь получить данные в ответ, я получаю сообщение об ошибке:
"Ошибка типа: не удается прочитать свойство 'map' of undefined"
Вот мой API:
router.get("/get_data", function (req, res) {
res.json({
"users": [
{
id: 1,
username: "samsepi0l"
},
{
id: 2,
username: "D0loresH4ze"
}
]
});
});
Вот код ReactJs:
import React, {Component} from "react";
import "./App.css";
class App extends Component {
state = {users: []};
componentDidMount() {
fetch("/funi/get_data")
.then(res => res.json())
.then(users => this.setState({users: users.username}));
}
render() {
return (<div className="App">
<h1>Users</h1>
{this.state.users.map((user, index) => <div key={index}>{user.username}</div>)}
</div>);
}
}
export default App;
Снимок экрана вызова API узла
![Screenshot of calling Node API](https://i.stack.imgur.com/AtiPj.png)