首页 > 解决方案 > 从 node.js 发送结果以做出反应

问题描述

因此listPage,在我editeditPage. 它现在确实这样做了。但我所做的是通过 发出请求axios.post,以便将id文档的 发送到后端,然后将结果发送到前端,在那里它只会​​根据它显示一个文档id。这就是我所拥有的:

列表页:

 const editById = (id) => {
console.log(id);
axios
  .post(`/getDocToEdit`, { id: id })
  .then(() => {
    console.log(id, " worked");
    window.location = "/admin/services/:site";
  })
  .catch((error) => {
    // Handle the errors here
    console.log(error);
  });

};

然后它到达这个后端路由:

    app.post('/getDocToEdit', (req, res) => {

    var id = req.body.id;
    ServicesModel.findOne({_id: id}, function(err,result) { 
        console.log(result); 
        res.status(200).send(result)
    });
})

然后我只是想在我的屏幕上显示文档editPage,但它不会加载我发送的结果res.status(200).send(result)。我只有一张应该显示记录的表格。我应该再次从前端打电话还是什么?

标签: javascriptnode.jsreactjsmongooseaxios

解决方案


您应该将发布结果保存在前端:


const editById = (id) => {
console.log(id);
axios
  .post(`/getDocToEdit`, { id: id })
  .then((RESPONSE) => {
    console.log(RESPONSE); // do it and if you have the response, everything 
    is fine and you can use it as the returned data
    console.log(id, " worked");
    window.location = "/admin/services/:site";
  })
  .catch((error) => {
    // Handle the errors here
    console.log(error);
  });

推荐阅读