我试了试:
{Object.keys(questions).map(
(question, index) => (
<div className="row questions" key={index}>
<p>{questions[question].question_title}</p>
// HERE NOT WORK
{Object.keys(questions[question].answers).map(
(answer, index) => (
console.log(questions[question].answers[answer].id_answer)
)
)}
</div>
)
)}
此返回"不能将未定义或null转换为对象">
此错误表明您有问题,其中answers
属性是undefined
或null
。这将导致调用Object.keys(undefined)
并抛出错误。
你需要为你的代码添加一个不执行的安全保护,如果是这种情况:
{ questions[question].answers && Object.keys(questions[question].answers).map(
(answer, index) => (
console.log(questions[question].answers[answer].id_answer)
)
)}