reactjs - 如何修复我在反应组件中遇到的语法错误?
问题描述
我按照教程创建了一个后端连接到 mongo 的全栈 react web 应用程序,现在在尝试将它与我以前的代码合并时,我收到了一个语法错误。
我尝试在谷歌搜索,但没有任何帮助
这是我的控制台错误
module build failed(from ./node_modules/babel-loader/lib/index.js);
syntaxError: c:/users/aviram/zofim/client/src/app.js: unexpected token (49:16)
49| getDataFromDb = () => {
这是我的代码
import React, { Component } from 'react';
import axios from 'axios';
class App extends Component {
// initialize our state
constructor(props){
super(props);
this.state = {
data: [],
id: 0,
message: null,
intervalIsSet: false,
idToDelete: null,
idToUpdate: null,
objectToUpdate: null
};
this.getDataFromDb = this.getDataFromDb.bind(this);
}
// when component mounts, first thing it does is fetch all existing data in our db
// then we incorporate a polling logic so that we can easily see if our db has
// changed and implement those changes into our UI
componentDidMount() {
this.getDataFromDb();
if (!this.state.intervalIsSet) {
let interval = setInterval(this.getDataFromDb, 1000);
this.setState({ intervalIsSet: interval });
}
}
// never let a process live forever
// always kill a process everytime we are done using it
componentWillUnmount() {
if (this.state.intervalIsSet) {
clearInterval(this.state.intervalIsSet);
this.setState({ intervalIsSet: null });
}
}
// just a note, here, in the front end, we use the id key of our data object
// in order to identify which we want to Update or delete.
// for our back end, we use the object id assigned by MongoDB to modify
// data base entries
// our first get method that uses our backend api to
// fetch data from our data base
getDataFromDb = () => {
fetch('http://localhost:3001/api/getData')
.then((data) => data.json())
.then((res) => this.setState({ data: res.data }));
};
我希望它编译
解决方案
你忘了在最后完成右括号 }。如果您使用箭头函数,则不需要绑定,因此删除此行 'this.getDataFromDb = this.getDataFromDb.bind(this);'
推荐阅读
- vue.js - 调整已绘制的矩形 paperjs 的大小
- python - 在具有不同日期的多列上合并或连接两个数据框
- python-3.x - DJANGO - 通过连接两个表从模型中获取值
- docker - 无法将 docker-compose 与覆盖网络一起使用
- elasticsearch - 如何在 ELK 堆栈中使用 APM jvm.memory.heap.used
- r - 有什么作用!!在函数内部的参数前面?
- regex - 获取字符串/和/之间的字符串
- python - 在另一个视图中使用一个视图中的值 Django
- caffe - 使用 caffe 训练 RCF 模型的问题
- react-native - 将多个导航器嵌套在堆栈中还是切换导航器更好?