我在努力弄清楚这里的逻辑..。
我有一个个人资料模型,其中包括工作经验。如果用户添加了一个唯一的沙龙( company ),我希望使用沙龙(Company)模式创建一个新公司。
用例是使用沙龙模型在沙龙的前端创建单独的页面。
const ProfileSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user'
},
...
experience: [
{
jobTitle: {
type: String,
},
jobRole: {
type: String,
}
jobSalon: {
type: mongoose.Schema.Types.ObjectId,
ref: 'salon'
},
jobFrom: {
type: Date,
required: true
},
jobTo: {
type: Date
},
jobCurrent: {
type: Boolean,
default: false
},
jobDescription: {
type: String
}
}
]
...
});沙龙模式
const SalonSchema = new mongoose.Schema({
salonCompany: {
type: String,
required: true
},
salonLocation: {
type: String,
required: true
}
})API添加体验路由
// @route PUT api/profile/experience
// @desc Add profile experience
// @access Private
router.put('/experience', [auth, [
check('jobRole', 'Role is required').not().notEmpty(),
check('jobFrom', 'Start date is required').not().notEmpty()
]], async (req, res) => {
const errors = validationResult(req)
if(!errors.isEmpty()){
return res.status(400).json({errors: errors.array})
}
const {
jobTitle,
jobRole,
jobSalon,
jobLocation,
jobFrom,
jobTo,
jobCurrent,
jobDescription
} = req.body
const newExp = {
jobTitle,
jobRole,
jobSalon,
jobLocation,
jobFrom,
jobTo,
jobCurrent,
jobDescription
}
try {
const profile = await Profile.findOne({ user: req.user.id })
profile.experience.unshift(newExp)
await profile.save()
res.json(profile)
} catch (err) {
console.error(err.message)
res.status(500).send('Server error')
}
})发布于 2021-01-19 13:16:09
不确定这是否是最优雅的解决方案,或适当的解决方案,但似乎是有效的。
在从体验中获得req.body之后,我正在运行一个检查,看看沙龙是否存在于沙龙数据库中,如果它存在,那么我继续,如果没有,它将创建一个新的沙龙。然后继续增加经验
const existingSalon = await Salon.findOne({ salonCompany: jobSalon })
if(!existingSalon){
// if no salon found
// create a new instance of salon
newSalon = new Salon({
salonCompany: req.body.jobSalon
})
// save the new salon in the database
await newSalon.save()
const jobSalon = newSalon
} else {
// if it does exist use it
const jobSalon = existingSalon
}在路由的上下文中:(认为我应该将上面的内容重构为中间件)
// @route PUT api/profile/experience
// @desc Add profile experience
// @access Private
router.put('/experience', [auth, [
check('jobRole', 'Role is required').not().notEmpty(),
check('jobFrom', 'Start date is required').not().notEmpty()
]], async (req, res) => {
const errors = validationResult(req)
if(!errors.isEmpty()){
return res.status(400).json({errors: errors.array})
}
const {
jobTitle,
jobRole,
jobSalon,
jobLocation,
jobFrom,
jobTo,
jobCurrent,
jobDescription
} = req.body
const existingSalon = await Salon.findOne({ salonCompany: jobSalon })
if(!existingSalon){
// if no salon found
// create a new instance of salon
newSalon = new Salon({
salonCompany: req.body.jobSalon
})
// save the new salon in the database
await newSalon.save()
const jobSalon = newSalon
} else {
// if it does exist use it
const jobSalon = existingSalon
}
const newExp = {
jobTitle,
jobRole,
jobSalon,
jobLocation,
jobFrom,
jobTo,
jobCurrent,
jobDescription
}
try {
const profile = await Profile.findOne({ user: req.user.id })
profile.experience.unshift(newExp)
await profile.save()
res.json(profile)
} catch (err) {
console.error(err.message)
res.status(500).send('Server error')
}
})https://stackoverflow.com/questions/65789723
复制相似问题