我是新开发人员ReactJS,我使用前端的ReactJS,后端的NodeJS和关于数据库的MySQL开发了一个表。我想通过列表中的按代码选择请求(主键)来获取数据。
如您在上方看到的,我想要,当我单击查看按钮时,它将定向我到列表页面,该页面将被检索。
我的ViewClient:
class ViewClient extends Component {
constructor(props) {
super(props);
this.state = {
clients: []
};
this.toggle = this.toggle.bind(this);
this.state = {
activeTab: '1',
};
}
toggle(tab) {
if (this.state.activeTab !== tab) {
this.setState({
activeTab: tab,
});
}
}
componentDidMount() {
var Code = this.props.Code;
axios({
method: "get",
url: "/app/viewclient/?Code="+Code+",
withCredentials: true,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
Accept: "application/json"
}
})
.then(response => {
if (response && response.data) {
this.setState({ clients: response.data });
}
})
.catch(error => console.log(error));
}
render() {
let { clients } = this.state;
return (
<div className="animated fadeIn">
<Row>
<Col xs="12" md="6" className="mb-4">
<Nav tabs>
<NavItem>
<NavLink
className={classnames({ active: this.state.activeTab === '1' })}
onClick={() => { this.toggle('1'); }}
>
<i className="fa fa-info"></i> <span className={this.state.activeTab === '1' ? '' : 'd-none'}> Détails</span>
</NavLink>
</NavItem>
<NavItem>
<NavLink
className={classnames({ active: this.state.activeTab === '2' })}
onClick={() => { this.toggle('2'); }}
>
<i className="fa fa-credit-card"></i> <span
className={this.state.activeTab === '2' ? '' : 'd-none'}> Factures</span>
</NavLink>
</NavItem>
<NavItem>
<NavLink
className={classnames({ active: this.state.activeTab === '3' })}
onClick={() => { this.toggle('3'); }}
>
<i className="fa fa-truck"></i> <span className={this.state.activeTab === '3' ? '' : 'd-none'}> Bons de livraison</span>
</NavLink>
</NavItem>
</Nav>
<TabContent activeTab={this.state.activeTab} style={{ height:"420px"}}>
<TabPane tabId="1">
<ul>
{
clients && clients.map(client => (
<li key={client.Code}>
<h1> Code client : {client.Code} </h1>
{client.Prenom}
{client.Nom}
{client.FAX}
{client.Telephone}
{client.Email}
{client.Adresse1}
{client.Adresse2}
</li>
))}
</ul>
</TabPane>
<TabPane tabId="2">
</TabPane>
<TabPane tabId="3">
</TabPane>
</TabContent>
</Col>
</Row>
</div>
);
}
}
export default ViewClient;
我的路由器是:
exports.viewclient = function(req, res) {
var Code = req.params.Code;
console.log(Code);
connection.query("SELECT Code, Prenom, Nom, FAX, Telephone, Email, Adresse1, Adresse2 FROM clients WHERE Code =?',[Code], function(error, results, fields) {
if (error) throw error;
res.send(JSON.stringify(results));
console.log(results);
});
}
我的服务器是:
router.get('/viewclient/:Code?', clients.viewclient);
运行它时,我进入控制台后端[]
和前端,但未显示任何内容,因为未检索到该行。
请问该如何解决?
答案 0 :(得分:1)
错误是,获取方法不包含正文参数。正文参数仅在POST请求中可用。尝试使用查询参数,因为您将代码发送为?Code=
。
在后端,使用req.query.Code
而不是req.body.Code