【发布时间】:2018-09-09 07:35:48
【问题描述】:
我试图从 mongodb/express 传递我的数据以使用 componentDidMount() 生命周期函数做出反应,但由于某种原因它不起作用,而是给了我这个 "Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0" 错误。
这是我设置快递的方式:
app.get('/movies', function(req, res){
movie.find({}, function(err, allMovies){
if(err){
console.log(err);
} else {
res.send(allMovies);
}
})
});
app.listen(3000, function(){
console.log('App server is listening to 3000');
});
在我的 react 文件中,我调用了 componentDidMount 函数,该函数通过 webpack 在 8080 端口运行。
componentDidMount() {
console.log('test')
fetch('/movies')
.then(response => response.json())
.then(movies => this.setState({
movies: movies
}));
}
当我在 console.log 响应时,它会返回 https://prnt.sc/iye6kk。
知道我做错了什么,为什么我的数据没有正确传递吗?
【问题讨论】: