我有一个 Express 端点,您可以 POST 到它,如下所示:
router.post("/add", (req, res) => {
Poll.create({
question: req.body.question,
options: req.body.options,
}).then(p => {
res.send(p);
});
});
这就是我要发布的内容:
{
"question": "what is your favourite colour?",
"options" :
[
{
"colour" : "green",
"votes" : 5
},
{
"colour": "red",
"votes": 50
}
]
}
我收到的回复是:
{
"__v": 0,
"question": "what is your favourite colour?",
"_id": "59fe97088687d4f91c2cb647",
"options": [
{
"votes": 5,
"_id": "59fe97088687d4f91c2cb649"
},
{
"votes": 50,
"_id": "59fe97088687d4f91c2cb648"
}
]
}
出于某种原因,“颜色”键没有被捕获。我通过查看 Mongo 中的集合证实了这一点,确实只有“投票”被捕获而没有颜色。
以防万一它在这里有帮助是模型架构:
const PollSchema = new Schema({
question: {
type: String,
},
options: [
{
option: {
type: String,
},
votes: Number,
},
],
});
如果您需要保存您没有在架构中计划的属性,您可以向其中添加该
{ strict: false }
选项。这样,属性将被保存。
const PollSchema = new Schema({
// ... your schema
}, { strict: false });
但如果您知道该属性将始终相同,最好将其添加到您的架构定义中。
const PollSchema = new Schema({
question: String,
options: [
{
colour: String,
votes: Number
}
]
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句