Javascript “如何修复”;未能编译";在我的书里?

Javascript “如何修复”;未能编译";在我的书里?,javascript,reactjs,Javascript,Reactjs,我一直在尝试制作一个reactjs网站,它显示编译失败。我得到了一些错误,这些状态和函数很少是未定义的,解决方案是什么。我把代码贴在下面。 我的代码所做的是,您输入标题和描述,然后当我单击save按钮时,它将保存帖子。我尝试将snippetDescription绑定到saving函数,但所有内容都未定义 import React, { Component, useState } from "react"; import { Link } from "react-router-dom"; impo

我一直在尝试制作一个reactjs网站,它显示编译失败。我得到了一些错误,这些状态和函数很少是未定义的,解决方案是什么。我把代码贴在下面。 我的代码所做的是,您输入标题和描述,然后当我单击save按钮时,它将保存帖子。我尝试将snippetDescription绑定到saving函数,但所有内容都未定义

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

handleRequest = async (postId) => {  //passed postId here
  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 = (postId) => {            //passed postId here
  if (postId == null) {
    return handleRequest("/api/savepost");
  }
  return handleRequest("/api/updatepost");
};
const analysis = (snippetDescription) => {   //passed snippetDescription
  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: "",
      textlen: 0
    };
  }

  render() {

    return (
      <>  
           <input
                type="text"
                id="titletext"
                placeholder="Enter title here"
                limit-to="64"
                className="inptxt"
                onChange={title => this.setState({ title })}
              ></input>
            <span class="itemcenter">              
              <Link to="/about">
                <Button
                  className="btn savebtn"
                  onClick={() => handlePost({ ...this.state })}
                >
                  Save <i className="fas fa-save" />
                </Button>
              </Link>
            </span>
            <textarea
              class="textareaclass"
              placeholder="Enter your text here"
              onChange={snippetDescription =>
                this.setState({ snippetDescription })
              }
            ></textarea>
     </>
     )}
我的状态是否未传递给函数?还有,我的函数handleRequest和handlePost是如何定义的

更新: 我已经移动了react组件中的所有代码(函数),添加了this.handlePost,它仍然显示相同的错误


./src/component/mainText.js
  Line 31:    'postId' is not defined              no-undef
  Line 32:   'postId' is not defined              no-undef
  Line 59:    'postId' is not defined              no-undef
  Line 60:   'handleRequest' is not defined       no-undef
  Line 62:   'handleRequest' is not defined       no-undef
  Line 134:  'snippetDescription' is not defined  no-undef
最新更新:

我现在已经编辑了函数,并将值传递给了之前在上述代码中没有传递的函数,所以现在我得到的唯一错误是handleRequest未定义。我应该在什么地方申报HandlerRequest吗

Line 60:   'handleRequest' is not defined       no-undef
Line 62:   'handleRequest' is not defined       no-undef
Updae:我添加了这个.handlerequest.bind(这个),这样它就不再显示错误了。我希望装订是对的。有人能核实一下吗

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

将错误中的所有相关代码移动到React组件的范围。不能在react组件类之外声明和使用函数和状态。 在组件的事件处理程序中,必须使用“this.”上下文调用组件函数。例如:

  onClick={() => this.handlePost....}

将错误中的所有相关代码移动到React组件的范围。不能在react组件类之外声明和使用函数和状态。 在组件的事件处理程序中,必须使用“this.”上下文调用组件函数。例如:

  onClick={() => this.handlePost....}

我将函数移到了react组件中,但仍然没有任何帮助。现在我得到了这个错误。第119行:分析错误:意外标记>119 |常量分析=()=>{我明白了,在函数的分析声明之前删除const。我如何更改onChange函数?我应该在哪里添加'this'?正如我在AnswerOche中所写的那样,我将函数移到react组件中,但仍然没有帮助。现在我得到了这个错误。第119行:解析错误:意外标记>119 | const分析=()=>{我明白了,在分析函数声明之前删除常量。我如何更改onChange函数?我应该在哪里添加“this”?正如我在answerochange中所写的那样,请在cod中声明缺少的所有变量。设置初始值。即:
postId
handleRequest
snippet说明
handleRequest
是函数,所以你可以声明空函数。我声明了snippetDescription,已经在状态中发布了。我应该在哪里再次声明它们呢?但是在你的示例中,我看不到它。请声明所有缺少的变量,在你的代码中。设置初始值。即:
posted
handleRequest
snippetDescription
HandlerRequest
是函数,因此您可以声明空函数我声明了snippetDescription,已在状态中发布。我应该在何处再次声明它们,但在您的示例中,我看不到它