首页 > 解决方案 > 如何将信息从 Node 传递到 React

问题描述

我有一个需要解析为 React 的对象。我正在尝试将“行”对象(在节点函数中)转移到反应状态。下面的 2 段代码在不同的页面上!

另一个问题是 GET http://localhost:3000/new net::ERR_CONNECTION_REFUSED

我目前在本地运行这两个 React http://localhost:3001/ 节点 - http://localhost:3000/

对此有类似的问题,但我找不到这两个问题的答案!谢谢

router.get("/new", (req, res) => {
let parentList = sql.fetchAllParents(function(err, rows) {
  res.setHeader("Access-Control-Allow-Origin", "http://localhost:3001");
    if (err) throw err;
    res.render('new', {parents: rows});
  });
});

componentDidMount() {
   fetch(`http://localhost:3000/new`).then(response => {
      console.log(response)
      return response.json();
   }).then(data => {
      // Work with JSON data here
      console.log(data,'data');
   }).catch(err => {
      // Do something for an error here
      console.log("Error Reading data " + err);
   });
}

标签: javascriptnode.jsreactjs

解决方案


从节点“新”api 发送响应。像这样:

router.get("/new", (req, res) => {
......
......
res.send({status:200,parents:data})

});


推荐阅读