我希望当我点击 Action 列上的查看按钮时,如下所示
它将被重定向到另一个页面,该页面显示一个包含 Select 查询结果的列表。代码如下:
我的路由器:
exports.viewclient = function(req, res) {
var Code = req.query.Code;
console.log(req.params);
connection.query('SELECT Code, Prenom, Nom, FAX, Telephone, Email, Adresse1, Adresse2 FROM clients WHERE Code = ?',[req.params.Code], function(error, results, fields) {
if (error) throw error;
res.send(JSON.stringify(results));
console.log(results);
});
}
我的服务器:
router.get('/viewclient/:Code', clients.viewclient);
我在类 Liste 上的 handleView 方法:
handleView(event) {
try {
console.log("Voir client")
this.props.history.push('/clients/viewclient/' + this.state.Code);
}
catch (error) {
this.setState({ error });
}
}
查看类:
constructor(props) {
super(props);
this.state = {
clients: [],
Code: this.props.match.params.Code
};
componentDidMount() {
axios.get('http://localhost:4000/app/viewclient/' + this.state._code)
.then(response => {
if (response && response.data) {
this.setState({ clients: response.data });
}
})
.catch(error => console.log(error));
}
当我使用 Postman http://localhost:4000/app/viewclient/1111
运行后端时,它返回 [{"Code":1111,"Prenom":"test""Nom":"test","FAX":"58985688888","Telephone":"58985699888","Email":"test@gmail.com","Adresse1":"","Adresse2":""}]
但是当我运行我的前端时,它会将我重定向到 http://localhost:3000/app/viewclient/undefined
而我无法查看 Select 的结果。
我该如何解决?
最佳答案
你的状态看起来像这样:
this.state = {
clients: [],
Code: this.props.match.params.Code
};
但是你这样称呼你的状态
this.state._code
你的状态变量的名称必须相同,所以改变你的状态变量或者你调用它们匹配:
this.state = {
clients: [],
Code: this.props.match.params.Code
};
axios.get('http://localhost:4000/app/viewclient/' + this.state.Code)
关于javascript - axios Get 请求问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51400194/