最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot set properties of undefined (setting 'duration') - Stack Overflow

programmeradmin0浏览0评论

I am getting this error when I am trying to add an answer although the error is negligible and my application works upon canceling the error and data is also getting saved in states and POST request is also happening. How to get rid of this?

Since on popping out of the error, I am able to perform my functionality accurately. Is there any way to track the issue or resolve it? thanks in advance.

import React, { useState } from "react";
import {
  Row,
  Col,
  Card,
  CardBody,
  FormGroup,
  Button,
  Label,
  Input,
  Container,
} from "reactstrap";
import { AvForm, AvField } from "availity-reactstrap-validation";
import RangeSlider from "react-bootstrap-range-slider";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faTimes } from "@fortawesome/free-solid-svg-icons";
import { connect } from "react-redux";
import { interviewAdd } from "../../redux/actions/interviewActions";
import Breadcrumbs from "../../ponents/Common/Breadcrumb";
import { Link, useHistory } from "react-router-dom";

const AddInterview = (props) => {
  const history = useHistory();
  const [breadcrumbItems, setBreadcrumbItems] = useState([
    { title: "Interview App", link: "#" },
    { title: "Interview Form", link: "#" },
  ]);
  const [name, setName] = useState("");
  const [tag, setTag] = useState([
    {
      name: "tech",
      value: "tech",
    },
    {
      name: "soft",
      value: "soft",
    },
  ]);

  const [Interviewer, setInterviewer] = useState({
    value: "",
  });

  const [q_id, setQuestion] = useState([{ q_id: "" }]);

  const interviewerTypes = [
    {
      name: "Male",
      value: "Male",
    },
    {
      name: "Female",
      value: "Female",
    },
  ];

  const [weightageValue, setWeightageValue] = useState(0);
  const [selectedQuestions, setSelectedQuestions] = useState([
    {
      Question: "",
      duration: "00:60",
    },
  ]);

  const onhandleFinalSubmit = (event, errors, values) => {
    event.preventDefault();
    console.log(q_id);
    const submit = {
      name,
      tag,
      questions: q_id,
      technical_weightage: weightageValue,
      soft_weightage: (100 - weightageValue).toString(),
      gender: Interviewer.value,
      customer_id: props.userId,
    };
    console.log("submit", submit);
    props.addInterview(submit, () => {
      history.push("/dashboard");
    });
  };

  const questionChange = (e, index) => {
    if (e.target.value !== "") {
      let temp = q_id;
      temp[index][e.target.name] = e.target.value;

      setQuestion([...temp]);
    }
  };

  const addQuestion = () => {
    setQuestion([...q_id, { q_id: "" }]);
  };

  const removeQuestion = (index) => {
    let temp = q_id;
    temp.splice(index, 1);

    setQuestion([...temp]);
  };

  const handleNameChange = (event) => {
    setName(event.target.value);
  };

  const handleTagChange = (event) => {
    setTag(event.target.value);
  };

  const handleSelectedQuestionsChange = (e, index) => {
    e.preventDefault();
    questionChange(e, index);
    const { name, value } = e.target;
    const listOfSelectedQuestions = [...selectedQuestions];
    console.log('value', value);
    listOfSelectedQuestions[index][name] = value;
    setSelectedQuestions(listOfSelectedQuestions);
  };

  const handleRemoveSelectedQuestions = (index) => {
    const list = [...selectedQuestions];
    list.splice(index, 1);
    setSelectedQuestions(list);
  };

  const handleInterviewerChange = (e) => {
    e.preventDefault();
    setInterviewer({
      value: e.target.value,
    });
  };

  return (
    <React.Fragment>
      <div className="page-content">
        <Container fluid={true}>
          <Breadcrumbs
            title="Interview Form"
            breadcrumbItems={breadcrumbItems}
          />
          <Row>
            <Col xl="12">
              <Card>
                <CardBody>
                  <AvForm className="needs-validation">
                    <Row>
                      <Col md="8">
                        <div className="mb-3">
                          <Label
                            className="form-label"
                            htmlFor="validationCustom01"
                          >
                            Name
                          </Label>
                          <AvField
                            name="Name"
                            placeholder="Enter Name"
                            type="text"
                            value={name}
                            onChange={handleNameChange}
                            errorMessage="Enter First Name"
                            className="form-control"
                            validate={{ required: { value: true } }}
                            id="validationCustom01"
                          />
                        </div>
                      </Col>
                      <Col md="4">
                        <div className="mb-3">
                          <Label
                            className="form-label"
                            htmlFor="validationCustom01"
                          >
                            Tag
                          </Label>
                          <FormGroup
                            row
                            className="mb-0"
                            value={tag.value}
                            onChange={handleTagChange}
                            validate={{ required: { value: true } }}
                            id="validationCustom01"
                          >
                            {" "}
                            <Col>
                              <select className="form-select">
                                <option defaultValue>Select tag</option>
                                <option value="tech">tech</option>
                                <option value="soft">soft</option>
                              </select>
                            </Col>
                          </FormGroup>
                        </div>
                      </Col>
                    </Row>
                    {q_id &&
                      q_id.map((questions, index) => (
                        <div className="mb-3">
                          <Row>
                            <Col md="4">
                              <Label
                                className="form-label"
                                htmlFor="validationCustom01"
                              >
                                Question
                              </Label>
                              <AvField
                                name="q_id"
                                placeholder="Enter question"
                                type="text"
                                className="form-control"
                                id="validationCustom01"
                                value={q_id[index].questions}
                                validate={{ required: { value: true } }}
                                onChange={(e) => questionChange(e, index)}
                              />
                            </Col>

                            <Col md="2">
                              <Label
                                className="form-label"
                                htmlFor="validationCustom01"
                              >
                                <br />
                              </Label>
                              <div className="mb-3">
                                <FormGroup>
                                  <Input
                                    Format="24"
                                    type="duration"
                                    name="duration"
                                    id="duration"
                                    value={questions.duration}
                                    validate={{ required: { value: true } }}
                                    onChange={(e) =>
                                      handleSelectedQuestionsChange(e, index)
                                    }
                                    placeholder="Enter Time"
                                    required
                                    maxLength="20"
                                  />
                                </FormGroup>
                              </div>
                            </Col>
                            <Col md="2">
                              <div className="mb-3">
                                <Label
                                  className="form-label"
                                  htmlFor="validationCustom01"
                                >
                                  <br />
                                </Label>
                                <FormGroup row className="mb-0">
                                  <Col>
                                    <Button
                                      color="primary"
                                      onClick={() => removeQuestion(index)}
                                    >
                                      Remove
                                    </Button>
                                  </Col>
                                </FormGroup>
                              </div>
                            </Col>
                            <Col md="2">
                              <div className="mb-2">
                                <Label
                                  className="form-label"
                                  htmlFor="validationCustom01"
                                >
                                  <br />
                                </Label>
                                <Col>
                                  <Button color="primary" onClick={addQuestion}>
                                    Add question
                                  </Button>
                                </Col>

                                {selectedQuestions.length == 1 ? null : (
                                  <Col md="1">
                                    <Button
                                      color="secondary"
                                      onClick={() =>
                                        handleRemoveSelectedQuestions(index)
                                      }
                                    >
                                      <FontAwesomeIcon icon={faTimes} />
                                    </Button>
                                  </Col>
                                )}
                              </div>
                            </Col>
                          </Row>
                        </div>
                      ))}

                    <Row>
                      <Col md="6">
                        <FormGroup>
                          <Label for="weightage">
                            Weightage of tech and soft skills
                          </Label>
                          <RangeSlider
                            className="form-control"
                            style={{ padding: 0 }}
                            tooltip="off"
                            value={weightageValue}
                            validate={{ required: { value: true } }}
                            id="validationCustom01"
                            errorMessage="Please provide weightage."
                            color="secondary"
                            onChange={(changeEvent) =>
                              setWeightageValue(changeEvent.target.value)
                            }
                          />
                          <Col sm="4">
                            <Label for="weightage">
                              {`Tech skills: ${weightageValue}%`}
                            </Label>
                            <Label for="weightage">
                              {`Soft skills: ${
                                weightageValue == 0 ? 0 : 100 - weightageValue
                              }%`}
                            </Label>
                          </Col>
                        </FormGroup>
                      </Col>
                    </Row>

                    <Row>
                      <Col md="6">
                        <div className="mb-3">
                          <FormGroup>
                            <Label
                              for="Interviewer"
                              className="form-label"
                              htmlFor="validationCustom01"
                            >
                              Interviewer
                            </Label>
                            <Input
                              type="select"
                              name="Interviewer"
                              id="Interviewer"
                              placeholder="Select Interviewer"
                              validate={{ required: { value: true } }}
                              value={Interviewer.value}
                              onChange={handleInterviewerChange}
                              required
                            >
                              <option selected disabled value="">
                                Select Interviewer Gender
                              </option>
                              {interviewerTypes &&
                              interviewerTypes.length > 0 ? (
                                interviewerTypes.map((interviewerType) => {
                                  return (
                                    <option
                                      value={interviewerType.value}
                                      key={interviewerType.value}
                                    >
                                      {interviewerType.name}
                                    </option>
                                  );
                                })
                              ) : (
                                <option disabled>
                                  No Interviewer Types Available
                                </option>
                              )}
                            </Input>
                          </FormGroup>
                        </div>
                      </Col>
                    </Row>
                    {/* <Link to="/dashboard"> */}
                      <Button
                        color="primary"
                        type="submit"
                        onClick={onhandleFinalSubmit}
                      >
                        Submit form
                      </Button>
                    {/* </Link> */}
                    <Link to="/dashboard">
                      <Button color="light" type="submit" className=" m-2">
                        Cancel
                      </Button>
                    </Link>
                  </AvForm>
                </CardBody>
              </Card>
            </Col>
          </Row>
        </Container>
      </div>
    </React.Fragment>
  );
};

const mapStateToProps = (state) => {
  console.log("state", state);
  return {
    userId: state.auth.user._id,
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    addInterview: (interviewObject, navigate) =>
      dispatch(interviewAdd(interviewObject, navigate)),
  };
};
export default connect(mapStateToProps, mapDispatchToProps)(AddInterview);

I am getting this error when I am trying to add an answer although the error is negligible and my application works upon canceling the error and data is also getting saved in states and POST request is also happening. How to get rid of this?

Since on popping out of the error, I am able to perform my functionality accurately. Is there any way to track the issue or resolve it? thanks in advance.

import React, { useState } from "react";
import {
  Row,
  Col,
  Card,
  CardBody,
  FormGroup,
  Button,
  Label,
  Input,
  Container,
} from "reactstrap";
import { AvForm, AvField } from "availity-reactstrap-validation";
import RangeSlider from "react-bootstrap-range-slider";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faTimes } from "@fortawesome/free-solid-svg-icons";
import { connect } from "react-redux";
import { interviewAdd } from "../../redux/actions/interviewActions";
import Breadcrumbs from "../../ponents/Common/Breadcrumb";
import { Link, useHistory } from "react-router-dom";

const AddInterview = (props) => {
  const history = useHistory();
  const [breadcrumbItems, setBreadcrumbItems] = useState([
    { title: "Interview App", link: "#" },
    { title: "Interview Form", link: "#" },
  ]);
  const [name, setName] = useState("");
  const [tag, setTag] = useState([
    {
      name: "tech",
      value: "tech",
    },
    {
      name: "soft",
      value: "soft",
    },
  ]);

  const [Interviewer, setInterviewer] = useState({
    value: "",
  });

  const [q_id, setQuestion] = useState([{ q_id: "" }]);

  const interviewerTypes = [
    {
      name: "Male",
      value: "Male",
    },
    {
      name: "Female",
      value: "Female",
    },
  ];

  const [weightageValue, setWeightageValue] = useState(0);
  const [selectedQuestions, setSelectedQuestions] = useState([
    {
      Question: "",
      duration: "00:60",
    },
  ]);

  const onhandleFinalSubmit = (event, errors, values) => {
    event.preventDefault();
    console.log(q_id);
    const submit = {
      name,
      tag,
      questions: q_id,
      technical_weightage: weightageValue,
      soft_weightage: (100 - weightageValue).toString(),
      gender: Interviewer.value,
      customer_id: props.userId,
    };
    console.log("submit", submit);
    props.addInterview(submit, () => {
      history.push("/dashboard");
    });
  };

  const questionChange = (e, index) => {
    if (e.target.value !== "") {
      let temp = q_id;
      temp[index][e.target.name] = e.target.value;

      setQuestion([...temp]);
    }
  };

  const addQuestion = () => {
    setQuestion([...q_id, { q_id: "" }]);
  };

  const removeQuestion = (index) => {
    let temp = q_id;
    temp.splice(index, 1);

    setQuestion([...temp]);
  };

  const handleNameChange = (event) => {
    setName(event.target.value);
  };

  const handleTagChange = (event) => {
    setTag(event.target.value);
  };

  const handleSelectedQuestionsChange = (e, index) => {
    e.preventDefault();
    questionChange(e, index);
    const { name, value } = e.target;
    const listOfSelectedQuestions = [...selectedQuestions];
    console.log('value', value);
    listOfSelectedQuestions[index][name] = value;
    setSelectedQuestions(listOfSelectedQuestions);
  };

  const handleRemoveSelectedQuestions = (index) => {
    const list = [...selectedQuestions];
    list.splice(index, 1);
    setSelectedQuestions(list);
  };

  const handleInterviewerChange = (e) => {
    e.preventDefault();
    setInterviewer({
      value: e.target.value,
    });
  };

  return (
    <React.Fragment>
      <div className="page-content">
        <Container fluid={true}>
          <Breadcrumbs
            title="Interview Form"
            breadcrumbItems={breadcrumbItems}
          />
          <Row>
            <Col xl="12">
              <Card>
                <CardBody>
                  <AvForm className="needs-validation">
                    <Row>
                      <Col md="8">
                        <div className="mb-3">
                          <Label
                            className="form-label"
                            htmlFor="validationCustom01"
                          >
                            Name
                          </Label>
                          <AvField
                            name="Name"
                            placeholder="Enter Name"
                            type="text"
                            value={name}
                            onChange={handleNameChange}
                            errorMessage="Enter First Name"
                            className="form-control"
                            validate={{ required: { value: true } }}
                            id="validationCustom01"
                          />
                        </div>
                      </Col>
                      <Col md="4">
                        <div className="mb-3">
                          <Label
                            className="form-label"
                            htmlFor="validationCustom01"
                          >
                            Tag
                          </Label>
                          <FormGroup
                            row
                            className="mb-0"
                            value={tag.value}
                            onChange={handleTagChange}
                            validate={{ required: { value: true } }}
                            id="validationCustom01"
                          >
                            {" "}
                            <Col>
                              <select className="form-select">
                                <option defaultValue>Select tag</option>
                                <option value="tech">tech</option>
                                <option value="soft">soft</option>
                              </select>
                            </Col>
                          </FormGroup>
                        </div>
                      </Col>
                    </Row>
                    {q_id &&
                      q_id.map((questions, index) => (
                        <div className="mb-3">
                          <Row>
                            <Col md="4">
                              <Label
                                className="form-label"
                                htmlFor="validationCustom01"
                              >
                                Question
                              </Label>
                              <AvField
                                name="q_id"
                                placeholder="Enter question"
                                type="text"
                                className="form-control"
                                id="validationCustom01"
                                value={q_id[index].questions}
                                validate={{ required: { value: true } }}
                                onChange={(e) => questionChange(e, index)}
                              />
                            </Col>

                            <Col md="2">
                              <Label
                                className="form-label"
                                htmlFor="validationCustom01"
                              >
                                <br />
                              </Label>
                              <div className="mb-3">
                                <FormGroup>
                                  <Input
                                    Format="24"
                                    type="duration"
                                    name="duration"
                                    id="duration"
                                    value={questions.duration}
                                    validate={{ required: { value: true } }}
                                    onChange={(e) =>
                                      handleSelectedQuestionsChange(e, index)
                                    }
                                    placeholder="Enter Time"
                                    required
                                    maxLength="20"
                                  />
                                </FormGroup>
                              </div>
                            </Col>
                            <Col md="2">
                              <div className="mb-3">
                                <Label
                                  className="form-label"
                                  htmlFor="validationCustom01"
                                >
                                  <br />
                                </Label>
                                <FormGroup row className="mb-0">
                                  <Col>
                                    <Button
                                      color="primary"
                                      onClick={() => removeQuestion(index)}
                                    >
                                      Remove
                                    </Button>
                                  </Col>
                                </FormGroup>
                              </div>
                            </Col>
                            <Col md="2">
                              <div className="mb-2">
                                <Label
                                  className="form-label"
                                  htmlFor="validationCustom01"
                                >
                                  <br />
                                </Label>
                                <Col>
                                  <Button color="primary" onClick={addQuestion}>
                                    Add question
                                  </Button>
                                </Col>

                                {selectedQuestions.length == 1 ? null : (
                                  <Col md="1">
                                    <Button
                                      color="secondary"
                                      onClick={() =>
                                        handleRemoveSelectedQuestions(index)
                                      }
                                    >
                                      <FontAwesomeIcon icon={faTimes} />
                                    </Button>
                                  </Col>
                                )}
                              </div>
                            </Col>
                          </Row>
                        </div>
                      ))}

                    <Row>
                      <Col md="6">
                        <FormGroup>
                          <Label for="weightage">
                            Weightage of tech and soft skills
                          </Label>
                          <RangeSlider
                            className="form-control"
                            style={{ padding: 0 }}
                            tooltip="off"
                            value={weightageValue}
                            validate={{ required: { value: true } }}
                            id="validationCustom01"
                            errorMessage="Please provide weightage."
                            color="secondary"
                            onChange={(changeEvent) =>
                              setWeightageValue(changeEvent.target.value)
                            }
                          />
                          <Col sm="4">
                            <Label for="weightage">
                              {`Tech skills: ${weightageValue}%`}
                            </Label>
                            <Label for="weightage">
                              {`Soft skills: ${
                                weightageValue == 0 ? 0 : 100 - weightageValue
                              }%`}
                            </Label>
                          </Col>
                        </FormGroup>
                      </Col>
                    </Row>

                    <Row>
                      <Col md="6">
                        <div className="mb-3">
                          <FormGroup>
                            <Label
                              for="Interviewer"
                              className="form-label"
                              htmlFor="validationCustom01"
                            >
                              Interviewer
                            </Label>
                            <Input
                              type="select"
                              name="Interviewer"
                              id="Interviewer"
                              placeholder="Select Interviewer"
                              validate={{ required: { value: true } }}
                              value={Interviewer.value}
                              onChange={handleInterviewerChange}
                              required
                            >
                              <option selected disabled value="">
                                Select Interviewer Gender
                              </option>
                              {interviewerTypes &&
                              interviewerTypes.length > 0 ? (
                                interviewerTypes.map((interviewerType) => {
                                  return (
                                    <option
                                      value={interviewerType.value}
                                      key={interviewerType.value}
                                    >
                                      {interviewerType.name}
                                    </option>
                                  );
                                })
                              ) : (
                                <option disabled>
                                  No Interviewer Types Available
                                </option>
                              )}
                            </Input>
                          </FormGroup>
                        </div>
                      </Col>
                    </Row>
                    {/* <Link to="/dashboard"> */}
                      <Button
                        color="primary"
                        type="submit"
                        onClick={onhandleFinalSubmit}
                      >
                        Submit form
                      </Button>
                    {/* </Link> */}
                    <Link to="/dashboard">
                      <Button color="light" type="submit" className=" m-2">
                        Cancel
                      </Button>
                    </Link>
                  </AvForm>
                </CardBody>
              </Card>
            </Col>
          </Row>
        </Container>
      </div>
    </React.Fragment>
  );
};

const mapStateToProps = (state) => {
  console.log("state", state);
  return {
    userId: state.auth.user._id,
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    addInterview: (interviewObject, navigate) =>
      dispatch(interviewAdd(interviewObject, navigate)),
  };
};
export default connect(mapStateToProps, mapDispatchToProps)(AddInterview);

Share Improve this question asked Mar 14, 2022 at 11:42 Fashad AhmedFashad Ahmed 1711 gold badge4 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

What is happening is that you are trying to access an item in the list using the index listOfSelectedQuestions[index] but the item doesn't exists.

What you can do is add an object to the index if it doesn't exists

if (listOfSelectedQuestions[index]) {
  // item exists, so only modify the property
  listOfSelectedQuestions[index][name] = value;
} else {
  // item doesn't exists, so add a new object entirely
  listOfSelectedQuestions[index] = {
    [name]: value
  }
}
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>