【问题标题】:Parsing error in a function in a reactjs appreactjs应用程序中的函数解析错误
【发布时间】:2019-10-14 06:02:00
【问题描述】:

我一直在制作一个小型 reactjs 应用程序,但在函数中出现解析错误。

这是我遇到的错误

  Line 104:  Parsing error: Unexpected token, expected ";"

  102 | //       });
  103 | //     });
> 104 | analysis(){  
      |           ^
  105 |   fetch('/api/analyse', {
  106 |       method: 'POST',
  107 |       body: JSON.stringify({

这是我放在渲染方法上面的函数。我使用了一个函数而不是 componentDidMount,因为当我单击分析按钮时会调用多个函数。这只是其中一个函数,每个函数都有同样的解析错误。

import React, { Component, useState } from "react";
import { Link } from "react-router-dom";
import { Button } from "react-bootstrap";

handleRequest = async () => {
  const post = this.state;

  const request = {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    }
  };

  if (postId != null) {
    post["id"] = postId;
  }

  try {
    const response = await fetch("/api/updateposts", {
      ...request,
      body: JSON.stringify(this.state)
    });
    const data = await response.json();

    if (data.status === 200) {
      console.log("success", "post saved successfully!");
    } else {
      console.log(
        "danger",
        "An error has occured while updating the post. Please try again"
      );
    }
  } catch (ex) {
    console.error(ex.stack);
    console.log(
      "danger",
      "An error has occured while updating the post. Please try again"
    );
  }
};

handlePost = () => {
  if (postId == null) {
    return handleRequest("/api/savepost");
  }
  return handleRequest("/api/updatepost");
};

analysis(){  
  fetch('/api/analyse', {
      method: 'POST',
      body: JSON.stringify({
        snippetdesc: 'snippetDescription'
      }),
      headers: {
        "Content-type": "application/json; charset=UTF-8"
      }
    })
    .then(response =>  response.json())
    .then((textdata) => {
        this.setState({
          textdata : textdata.data,
          textlen : snippetDescription.split(' ').length
        });
      },(error) => {
            console.log(error)
      })
  }

export default class MainText extends Component {
  constructor(props) {
    super(props);
    this.state = {
      title: "",
      description: "",
      id: null,
      snippetDescription: "",
      textdata: [],
      textlen: 0
      loadingautocorrection: true;
    };
  }

  render() {
    return (
      <>
               <Button
                  className="btn savebtn"
                  onClick={() => handlePost({ ...this.state })}
                >
                  Save <i className="fas fa-save" />
                </Button>
      </>
)}

【问题讨论】:

  • 答案在于你之前的函数。也许您没有正确关闭所有内容?
  • 看一下编辑后的帖子,我也添加了以前的功能。我想我已经关闭了一切
  • 我应该把它放在'export default class Text extends Component'里面吗?函数可以在类之外,对吧?
  • 这个analysis 构造在哪里?它是在class 中还是单独存在?
  • 在它自己的。不在课堂内。我应该把它放在课堂上吗? @T.J.克劳德

标签: javascript reactjs


【解决方案1】:

你忘了把function关键字放在函数名之前:

function analysis() {
... 
}

const analysis = () => {
...
}

【讨论】:

  • 或使用const analysis = () =&gt; { ...
  • 是的,添加这个已经消除了那个错误,现在我得到了其他错误。无论如何,我认为您的解决方案有效。谢谢@FrankerZ
  • @henrydoe - 问题不应该是移动目标。您询问了一个特定问题,上面的答案向您展示了如何纠正。如果您在编写 React 组件时遇到更普遍的问题,请仔细查看几个示例和教程,如果您仍然无法弄清楚,请发布一个包含您正在尝试的细节的问题要做的事情,你看到了什么,你遇到了什么问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
相关资源
最近更新 更多