我有一个这样的模式
{name :string,
Answers : [{
content:string,
choice:[string]}]
} 在我的请求中,我有一个json
{"name":"fooo",
"answers":[{
"content" :"hi",
"choice":["1","2","3"]},
{content":"hello",
"choice ":["0","25","3"]}
}] 我不知道如何在mongodb中存储"answers“数组的数据,因为当我这样做时
Const test =new Test({
name:req.body.name,
answers:[{
content :req.body.answers.content,
choice:req.body.answers.choise
}
]})它抛给我一个错误内容是必需的,但当我发送一个post请求时,一个白色的回答,它工作并存储在数据库中
发布于 2021-04-28 17:40:31
您的请求正文将answers作为数组
{
"name":"fooo",
"answers":[
{ "content" :"hi", "choice":["1","2","3"] },
{ "content":"hello", choice ":["0","25","3"] }
]
}您必须使用像req.body.answers[0].content这样的索引来访问数组值
在这里您可以直接使用answers: req.body.answers,它将指向您的请求主体answers数组。
https://stackoverflow.com/questions/67297427
复制相似问题