如何让 body-parser 将数据解析为数组模式?
How to have body-parser parse data into a schema of arrays?
你能帮我解决这个问题吗?
我的猫鼬模式看起来像这样
var TheSchema = new Schema({
name: 'String',
givenTask: [{
today: 'String',
tomorrow: 'String'
}]
});
如何让 body-parser 将此数据解析为 MongoDB?我一直在尝试这个:
.post(function(req, res) {
var schema = new TheSchema();
schema.name = req.body.name;
schema.givenTask.today = req.body.today;
schema.givenTask.tomorrow = req.body.tomorrow;
schema.save(function(err) {
});
});
使用 Postman 有什么需要注意的地方吗?我以为我会像这样命名 body 字段:
名称:
现在:
后来:
你能纠正我吗?非常感谢。
要插入 givenTask
数组,您需要对其进行初始化,然后将对象推入其中。
var schema = new TheSchema();
schema.name = req.body.name;
schema.givenTask = [];
schema.givenTask.push({today: req.body.today, tomorrow: req.body.tomorrow});
schema.save(function(err) {
});
你能帮我解决这个问题吗?
我的猫鼬模式看起来像这样
var TheSchema = new Schema({
name: 'String',
givenTask: [{
today: 'String',
tomorrow: 'String'
}]
});
如何让 body-parser 将此数据解析为 MongoDB?我一直在尝试这个:
.post(function(req, res) {
var schema = new TheSchema();
schema.name = req.body.name;
schema.givenTask.today = req.body.today;
schema.givenTask.tomorrow = req.body.tomorrow;
schema.save(function(err) {
});
});
使用 Postman 有什么需要注意的地方吗?我以为我会像这样命名 body 字段: 名称: 现在: 后来:
你能纠正我吗?非常感谢。
要插入 givenTask
数组,您需要对其进行初始化,然后将对象推入其中。
var schema = new TheSchema();
schema.name = req.body.name;
schema.givenTask = [];
schema.givenTask.push({today: req.body.today, tomorrow: req.body.tomorrow});
schema.save(function(err) {
});