我想在回答谜题后将其删除,以使登录的用户无法始终回答同一谜题,问题是当它传递ID时,这些ID是不确定的。我不知道我在哪里出错,这是我的代码的一部分。 他在控制台中收到的错误 错误:DELETE http:// api /删除/未定义400(错误请求)
state={
qBank:[{id:1,question:'dd',answers:['a','b','c','d'],correct:'a'}]
}
渲染:
this.state.qBank.map(
({ question, answers, correct, id }) => (
<QuestionBox key={id} id={id} question={question} options={answers} selected={answer => this.computeAnswer(answer, correct)} />
)
)}
QuestionBox:
const QuestionBox = ({ question, options, selected, key, id }) => {
const [answer, setAnswer] = useState(options);
const [alreadyAnswered, setAlreadyAnswered] = useState(false);
return (
<div className="questionBox">
<div className="question">{question}</div>
{(answer || []).map((text, index) => (
<button disabled={alreadyAnswered} key={index} className="answerBtn" onClick={(id) => {
if (!alreadyAnswered) {
setAnswer([text]);
selected(text);
setAlreadyAnswered(true);
fetch("http:/api/Remove/" + id, {
method: 'DELETE',
headers: {
"Content-Type": "application/json",
"Authorization": `bearer ${sessionStorage.getItem("access_token")}`
},
}).then(() => {
}).catch(err => {
console.error(err)
})
}
}}>{text}</button>
))}
</div>
)
}