我试图让我站点的用户创建他们自己的测验,但是我对如何最好地将他们的测验纳入MongoDB遇到了问题。
一个问题是用户提交的测验可以包含任意数量的问题,因此我认为用req.body专门引用每个问题和答案是没有道理的。这意味着给每个问题并回答一个唯一的名称属性,如下所示:
Question:<input type="text" name="question1">
<br>answer:<input type="text" name="ans1a">
<br>answer:<input type="text" name="ans1b">
<br>answer:<input type="text" name="ans1c">
Question:<input type="text" id="question2">
<br>answer:<input type="text" name="ans2a">
<br>answer:<input type="text" name="ans2b">
<br>answer:<input type="text" name="ans2c">
用户可以通过单击“新问题”按钮添加更多问题,因此测验可能会很长。
最终,我希望用户创建的测验以如下格式存储在MongoDB中:
[{
"question": "Which of these involves the analysis of of a business's financial statements, often used in stock valuation?",
"choices": ["Fundamental analysis", "Technical analysis"],
"correct": 0
}, {
"question": "What was the name of the bond purchasing program started by the U.S. Federal Reserve in response to the 2008 financial crisis?",
"choices": ["Stimulus Package", "Mercantilism", "Quantitative Easing"],
"correct": 2
}, {
"question": "Which term describes a debt security issued by a government, company, or other entity?",
"choices": ["Bond", "Stock", "Mutual fund"],
"correct": 0
}]
在此先感谢您的指导。
据我了解,@ jake1986不想为每个问题在req.body上设置单独的属性;他希望在单个数据结构中使用它。
如果是这种情况,我可能会为前端编写一个函数,以将具有您所描述的mongo条目之类的属性的问题对象组合起来,并将它们全部放入一个数组中。该数组将在POST请求中发送。您可以通过将此数组传递给create()函数,将它们作为文档添加到Mongo。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句