javascript - 如何将信息从 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);
});
}
解决方案
从节点“新”api 发送响应。像这样:
router.get("/new", (req, res) => {
......
......
res.send({status:200,parents:data})
});
推荐阅读
- visual-studio-code - Visual Studio Code 的 PHP Doc Blocker 无法正常工作
- ios - 如何根据设备方向更改堆栈视图方向?
- twitter-bootstrap - Bootstrap 的可折叠菜单不起作用
- google-cloud-platform - Google Compute Engine 上的 A2 区域配额限制不会增加
- reactjs - 如何解决未安装组件的状态更新(挂钩)
- node.js - 如何在 mongodb 和 mongoose 模型中使用 cloudinary
- javascript - iframe 内部链接到嵌入的网站
- python - 在后台线程中更新 SQLAlchemy 中的记录?
- javascript - 为什么我得到:TypeError:无法读取未定义的属性“toLowerCase”(React JS)
- javascript - 为什么事件监听器会执行以下代码?