javascript - axios Get 请求问题

标签 javascript mysql node.js reactjs axios

我希望当我点击 Action 列上的查看按钮时,如下所示

enter image description here

它将被重定向到另一个页面,该页面显示一个包含 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/

相关文章:

javascript - 在 md-autocomplete 上使用 ng-blur?

php - MYSQL Max(column) 其中 2 个表具有相同的列

mysql - 通过将 2 个表与 POST 变量进行比较来显示可用时间

连接2个表的mysql命令

node.js - Sequelize get random entry with association and limit, "缺少FROM子句条目

javascript - 如何欺骗 Node.js 将 .js 文件加载为 ES6 模块?

javascript - 如何在悬停 div2 时显示 div1 并在 mouseout 时显示淡出的 div1?

javascript - 使用 React Hooks 和 Ant Design 上传文件

JavaScript 粘贴和多个剪贴板级别

node.js - 使用nodejs和jspdf在pdf中添加png图像