首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建一个“新公司”,如果在工作经验中没有发现一个公司

创建一个“新公司”,如果在工作经验中没有发现一个公司
EN

Stack Overflow用户
提问于 2021-01-19 10:32:03
回答 1查看 20关注 0票数 0

我在努力弄清楚这里的逻辑..。

我有一个个人资料模型,其中包括工作经验。如果用户添加了一个唯一的沙龙( company ),我希望使用沙龙(Company)模式创建一个新公司。

用例是使用沙龙模型在沙龙的前端创建单独的页面。

代码语言:javascript
复制
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
      }
    }
  ]
...
});

沙龙模式

代码语言:javascript
复制
const SalonSchema = new mongoose.Schema({
        salonCompany: {
                type: String,
                required: true
        },
        salonLocation: {
                type: String,
                required: true
        }
})

API添加体验路由

代码语言:javascript
复制
// @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')
    }
})
EN

回答 1

Stack Overflow用户

发布于 2021-01-19 13:16:09

不确定这是否是最优雅的解决方案,或适当的解决方案,但似乎是有效的。

在从体验中获得req.body之后,我正在运行一个检查,看看沙龙是否存在于沙龙数据库中,如果它存在,那么我继续,如果没有,它将创建一个新的沙龙。然后继续增加经验

代码语言:javascript
复制
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
        }

在路由的上下文中:(认为我应该将上面的内容重构为中间件)

代码语言:javascript
复制
// @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')
    }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65789723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档