【发布时间】:2020-11-01 12:06:45
【问题描述】:
我正在尝试学习如何在 express/router 和 Mongoose 中创建规范化的多对多 Post 请求。
我有三个集合:User、Building 和 Room,这也是父文档到子文档的顺序。
我的 Building 文档架构包括以下 User 和 Room id:
const mongoose = require('mongoose');
const BuildingSchema = new mongoose.Schema({
user: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "user"
}
],
room: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "room"
}
],
My Room 文档架构包括 Building ID 和另一个子文档,如下所示:
const mongoose = require('mongoose');
const RoomSchema = new mongoose.Schema({
building: {
type: mongoose.Schema.Types.ObjectId,
ref: "building"
},
furniture: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "furniture"
}
],
我无法理解如何创建一个发布请求,以允许用户创建一个 Building 实例和多个与之关联的 Room 实例...
到目前为止,我的快速代码可以创建一个新的 Building 实例,但我不确定如何处理包括 Room id 的:
router.post('/createbuilding', [auth, [
check('name', 'Building Name is required').not().isEmpty(),
]
], async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const {
name,
type,
website,
location,
bio,
} = req.body;
const buildingFields = {
user: req.user.id,
//room: req.room.id,
name,
type,
website: website && website !== '' ? normalize(website, { forceHttps: true }) : '',
location,
bio
};
try {
let building = await Building.findOneAndUpdate(
{ user: req.user.id },
//{ room: req.room.id },
{ $set: buildingFields },
{ new: true, upsert: true }
);
res.json(building);
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
}
);
注意:对房间 ID 的引用是故意注释掉的,因为我不确定如何将其包含在 Post 请求中。
【问题讨论】:
标签: node.js express post mongoose router