我是reactjs的新开发者,我开发了一个表,前台是reactjs,后台是nodejs,数据库是mysql。我想用select请求在表上获取一个数据,我用postaman在后端测试了它,效果很好。
我的前端:
class ListeClients extends Component {
constructor(props) {
super(props);
this.state = {
clients: []
};
}
componentDidMount() {
console.log("inside componentWillMount");
let self = this;
axios({
method: "get",
url: "/app/listeclients/",
withCredentials: true,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
Accept: "application/json"
}
})
.then(response => {
// const clients = response.data;
console.log(JSON.stringify(response.data));
self.clients = response.data;
// this.setState({clients});
})
.catch(error => {
console.log(error);
});
}
render() {
console.log(this.state.clients);
return (
<div className="animated fadeIn">
<Row>
<Col>
<Card>
<CardHeader>
<h4>
<strong>
<i className="fa fa-align-justify" /> Tous les clients
</strong>
</h4>
</CardHeader>
<CardBody>
<div className="container">
<div className="panel panel-default p50 uth-panel">
<table className="table table-hover">
<thead>
<tr>
<th>Code</th>
<th>Prenom</th>
<th>Nom</th>
<th>Email</th>
<th>Telephone</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{this.state.clients.map(client => (
<tr key={client.id}>
<td>{client.Code} </td>
<td>{client.Prenom}</td>
<td>{client.Nom}</td>
<td>{client.Email}</td>
<td>{client.Telephone}</td>
<td>
<a>Edit</a>|<a>Delete</a>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</CardBody>
</Card>
</Col>
</Row>
</div>
);
}
}
export default ListeClients;
但当我在reactjs上运行时,我得到:
[]
length: 0__proto__: Array(0)concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}__proto__: Object
ListeClients.js:15 inside componentWillMount
我想得到这个表上的数据:
请问怎么修?
3条答案
按热度按时间cu6pst1q1#
尝试使用默认的axios get()方法:
默认情况下,它应该可以很好地处理json数据。
dbf7pr2w2#
您的组件应该是这样的:
你真的不必申报
self = this
. 毫无意义。您可以直接变异/更改状态属性,而不是使用
this.setState
react提供的方法。你不应该直接改变状态,除非它是
constructor
.ltqd579y3#
尝试使用完整的url:
我想因为这个你得到的是一个空洞的结果。