【发布时间】: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