我正在关注MERN stack
上的教程,我们创建了一条增加用户体验的途径
当我使用邮递员将任何内容添加到体验字段时,都会生成一个附加的id字段。 如何生成ID?。如果经验是不同的架构,并且我已将其链接到配置文件架构,则可能会生成id,但在这种情况下,我认为没有任何理由。 enter image description here enter image description here
配置文件架构
const mongoose=require('mongoose')
const ProfileSchema=new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,ref: 'user'
},company: {
type: String
},website: {
type: String
},location: {
type: String
},status: {
type: String,required: true
},skills: {
type: [String],bio: {
type: String
},githubusername: {
type: String
},experience: [
{
title: {
type: String,required: true
},company: {
type: String,location: {
type: String
},from: {
type: Date,to: {
type: Date
},current: {
type: Boolean,default: false
},description: {
type: String
}
}
],education: [
{
school: {
type: String,degree: {
type: String,fieldofstudy: {
type: String,social: {
youtube: {
type: String
},twitter: {
type: String
},facebook: {
type: String
},linkedin: {
type: String
},instagram: {
type: String
}
},date: {
type: Date,default: Date.now
}
})
module.exports = Profile = mongoose.model('profile',ProfileSchema);
增加经验的途径
router.put('/experience',[auth,[
check('title',"title is required").not().isEmpty(),check('company',"company is required").not().isEmpty(),check('from',"from date is required").not().isEmpty()
] ],async(req,res)=>{
const errors=validationResult(req)
if(!errors.isEmpty()){
return res.status(400).json({errors:errors.array()})
}
const {
title,company,location,from,to,current,description
}=req.body
const newExp={
title,description
}
try{
const profile=await Profile.findOne({user:req.user.id})
profile.experience.unshift(newExp)
await profile.save()
res.json(profile)
}catch(e){
console.log(e.message)
res.status(500).send('server error')
}
})