下面是我的每月预算集合的架构。他们每个人被分配一年零一个月。每个预算中都有一个多个类别和子类别。我决定将它们存储在数组中。
var {mongoose} = require('../db/mongoose');
var mb = new mongoose.Schema({
month: String,
categories: [{
name: String,
sub_categories: [{
name: String,
amount: Number
}]
}]
});
var Monthly_Budgets = mongoose.model('Monthly_Budgets', mb);
module.exports = {
Monthly_Budgets
};
以下是邮政特快专递请求
app.post('/monthlyBudgets', (req, res) => {
var sub_categories = req.body.categories.sub_categories;
var categories = [{
name : req.body.categories.name,
sub_categories
}]
var budgets = new Monthly_Budgets({
month : req.body.month,
year: req.body.year
})
budgets.categories = categories;
budgets.save().then((docs) => {
res.send(docs);
console.log(docs);
}).catch((e) => res.status(404).send(e));
})
当我从邮递员发送发帖请求时,它不会导致错误,但是会显示以下内容:
{
"_id" : ObjectId("5b8a3280924c2d0dea15a1df"),
"month" : "September",
"year" : 2018,
"categories" : [
{
"_id" : ObjectId("5b8a3280924c2d0dea15a1e0"),
"sub_categories" : []
}
],
"__v" : 0
}
我似乎无法弄清楚这个问题。请帮忙。