【发布时间】:2019-10-26 05:19:03
【问题描述】:
问题:我正在使用 fetch API 向节点发送 get 请求,但出现以下错误
阅读代码中的注释
server.js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const notes = [
{id: 0, title: "Dinner", body: "- Chicken - Rice - Potatos"},
{id: 1, title: "Dinner", body: "- Chicken - Rice - Potatos"}
];
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res, next)=>{
// Sending data!
res.json(notes);
})
const port = 3001;
app.listen(port, ()=> console.log(`Server started on port ${port}`));
app.js
class App extends Component{
state = {
notes: []
}
componentDidMount(){
// Line where I get the error
fetch('/')
.then(resp => resp.json())
.then(data => this.setState({notes: data}))
};
【问题讨论】:
-
在
fetch()承诺链的末尾放置一个.catch()处理程序并记录它报告的确切错误。无论如何,您应该始终有一个.catch()处理程序。
标签: javascript reactjs express fetch