TypeError:无法读取未定义的属性“地图”错误

时间:2020-06-29 09:47:34

标签: javascript reactjs

我想制作一个测验Web应用程序,每当我在update tests set name = 'b' where id = '6'; -- if id is integer use id = 6 中添加地图功能时,都会出现错误。

有人可以解决这个问题吗?

错误:

QuestionBox.js

4 | const [answer, setAnswer] = useState(options); 5 | return ( 6 | <div className="questionBox"> > 7 | <div className="question">{question}</div> | ^ 8 | {answer.map((text, index) => ( 9 | <button 10 | key={index} 的代码:

QuestionBox.js

3 个答案:

答案 0 :(得分:1)

{this.state.questionBank.length > 0 && this.state.questionBank.map( ({question,answer,correct,questionId}) => ( ) ) } –

这里的答案应该是 QuizService 中定义的答案

这是正确的 QuestionBox 道具

{this.state.questionBank.length > 0 && this.state.questionBank.map( ({question,answers,correct,questionId}) => ( ) ) } –

答案 1 :(得分:0)

我收到此错误是因为渲染optionsQuestionBox未定义。您应该尝试这样做:

import React, {useState} from "react";

const QuestionBox = ({question, options = [], selected}) => {
  const [answer, setAnswer] = useState(options);
  return (
    <div className="questionBox">
      <div className="question">{question}</div>
      {answer && answer.map((text, index) => (
        <button
          key={index}
          className="answerBtn"
          onClick={() => {
            setAnswer([text]);
            selected(text);
          }}
        >
          {text}
        </button>
      ))}
    </div>
  );
};

export default QuestionBox;

答案 2 :(得分:0)

将默认的空数组分配给question参数。它将首次解决该问题。