首页 > 解决方案 > 如何修复我在反应组件中遇到的语法错误?

问题描述

我按照教程创建了一个后端连接到 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 }));
};

我希望它编译

标签: reactjs

解决方案


你忘了在最后完成右括号 }。如果您使用箭头函数,则不需要绑定,因此删除此行 'this.getDataFromDb = this.getDataFromDb.bind(this);'


推荐阅读