【问题标题】:how can I store 4input data to anoptions array?如何将 4input 数据存储到选项数组?
【发布时间】:2021-04-27 16:39:30
【问题描述】:

这是我的反应代码,我想使用反应表单创建民意调查,但我不明白我的代码出了什么问题......!`这里我有 4 个输入字段 { option1: "", option2: " ", option3: "", option4: "" },但我不知道如何存储数据,就像我使用 POSTMAN 存储数据一样......!任何人都可以帮助请帮助....!请帮助我不明白该怎么办...!使用我的代码 请帮忙......

import React, { useState, useEffect } from "react";
import "../styles.css";
import { isAutheticated } from "../auth/helper/index";
import { createaPoll } from "./helper/adminapicall";

const AddPoll = () => {
  const { user, token } = isAutheticated();
  const [value, setValue] = useState({
    question: "",
    options: { option1: "", option2: "", option3: "", option4: "" },
    error: "",
    loading: "false",
    getRedirect: false,
    formData: "",
  });

  const { question, options, error, loading, getRedirect, formData } = value;

  const handleChange = (event) => {
    // setError("");
    setValue({ ...value, [event.target.name]: event.target.value });
    // console.log(event.target.value);
    const newOption = {
      ...value.options,
      [event.target.name]: event.target.value,
    };
    setValue((prev) => ({ ...prev, options: newOption }));
  };

  const onSubmit = (event) => {
    event.preventDefault();
    setValue({ ...value, error: "", loading: true });
    // console.log(handel);
    createaPoll(user._id, token, { question, options }).then((data) => {
      if (data.error) {
        setValue({ ...value, error: data.error });
      } else {
        setValue({
          ...value,
          question: "",
          options: { option1: "", option2: "", option3: "", option4: "" },
          error: "",
          loading: "false",
          getRedirect: false,
          formData: "",
        });
      }
    });
  };

  return (
    <div className="AddPoll">
      <div className="container">
        <h1>Add New Poll</h1>
        <form>
          <textarea
            rows="4"
            cols="50"
            className="form-control mb-2"
            placeholder="Question"
            name="question"
            value={question}
            onChange={(event) => handleChange(event)}
            autoFocus
          ></textarea>
          <input
            type="text"
            className="form-control mb-2"
            placeholder="Option1"
            onChange={(event) => handleChange(event)}
            name="option1"
            value={options.option1}
          />
          <input
            type="text"
            className="form-control mb-2"
            placeholder="Option2"
            onChange={(event) => handleChange(event)}
            name="option2"
            value={options.option2}
          />
          <input
            type="text"
            className="form-control mb-2"
            placeholder="Option3"
            onChange={(event) => handleChange(event)}
            name="option3"
            value={options.option3}
          />
          <input
            type="text"
            className="form-control mb-2"
            placeholder="Option4"
            onChange={(event) => handleChange(event)}
            name="option4"
            value={options.option4}
          />
          <button type="submit" onClick={onSubmit} className="btn Submitbtn">
            Submit
          </button>
        </form>
      </div>
    </div>
  );
};

export default AddPoll;

当我使用 POSTMAN 时,它工作正常!这是我的邮递员图片

所以我不明白该怎么做,我的反应表单代码,任何人都可以帮助我......!

【问题讨论】:

  • 能否添加createaPoll函数?

标签: node.js reactjs react-native express mern


【解决方案1】:

尝试在handleChange 函数中分离questionoption 值。

const handleChange = event => {
  if (event.target.name === 'question') {
    setValue({ ...value, [event.target.name]: event.target.value });
  } else {
    const newOption = {
      ...value.option,
      [event.target.name]: event.target.value
    };
    setValue({ ...value, option: newOption });
  }
};

【讨论】:

  • 未处理的拒绝(TypeError):无法读取未定义的属性“错误”,,,! setValue({ ...value, error: "", loading: true }); 34 | // console.log(handel); 35 | createaPoll(user._id, token, { question, option }).then((data) => { > 36 | if (data.error) { | ^ 37 | setValue({ ...value, error: data.error }); 38 | } 其他 { 39 | setValue({
【解决方案2】:

试试这个:

  const [value, setValue] = useState({
    question: "",
    option: [ option1: "", option2: "", option3: "", option4: "" ],
    error: "",
    loading: "false",
    getRedirect: false,
    formData: "",
  });

 


const onSubmit = (event) => {
    event.preventDefault();
    setValue({ ...value, error: "", loading: true });
    // console.log(handel);
    createaPoll(user._id, token, { question, option }).then((data) => {
      if (data.error) {
        setValue({ ...value, error: data.error });
      } else {
        setValue({
          ...value,
          question: "",
          option: ["hello","hi","hey"],
          error: "",
          loading: "false",
          getRedirect: false,
          formData: "",
        });
      }
    });
  };

【讨论】:

  • 第 10:15 行:'option1' 未定义 no-undef 第 10:30 行:'option2' 未定义 no-undef 第 10:45 行:'option3' 未定义 no-undef第 10:60 行:'option4' 未定义 no-undef 第 41:20 行:'option1' 未定义 no-undef 第 41:29 行:'option2' 未定义 no-undef 第 41:38 行:'option3' is not defined no-undef Line 41:47: 'option4' is not defined
  • STILL GETTING Line 10:15: 'option1' is not defined no-undef Line 10:30: 'option2' is not defined no-undef Line 10:45: 'option3' is not defined no -undef 第 10:60 行:'option4' 未定义
  • ReferenceError: 在初始化 const { user, token } = isAutheticated(); 之前无法访问“值” 8 | const [value, setValue] = useState({ 9 | question: "", > 10 | option: [ | ^ 11 | value.option.option1, 12 | value.option.option2, 13 | value.option.option3,
  • ReferenceError: 在初始化 const [value, setValue] = useState({ 9 | question: "", > 10 | option: [ | ^ 11 | value ? value.option. option1 : "", 12 | value ? value.option.option2 : "", 13 | value ? value.option.option3 : "",
  • 将选项的值设置为你在邮递员中设置的参数,以确保问题出在此处或其他地方。
猜你喜欢
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 2013-07-03
  • 1970-01-01
相关资源
最近更新 更多