E11000重复键错误集合:housing.posts索引:likes_1 dup键:{喜欢:未定义}

时间:2020-09-21 06:21:26

标签: javascript node.js mongodb mongoose

尝试一次将新帖子添加到数据库中,一次添加,第二次则提示错误 发布架构如下

const PostSchema:Schema = new Schema({
    author:{required:true,
      type:mongoose.Schema.Types.ObjectId,
      ref:'User',
    },
    title:{required:false,type:String},
    image:{required:false,type:String,},
    contents:{required:true,type:String},
    tags:{required:false,type:[String]},
    likes:{type:[
      {
        type:mongoose.Schema.Types.ObjectId,
        ref:'User',
      }
    ],
        default:[],
        unique:false,
        required:true
      },
    createdAt:{type:Date,default:Date.now},
    public:{type:Boolean,default:true},
})

发布功能在这里

   request.body.author=request.body.AuthorizedUser.id
   let {author,tags,title,contents}=request.body
   tags=tags?tags:[]
   const data= await PostModel.create({author,tags,title,contents,likes:[]}) 
   response.status(201).json({success: true,data})   
   

0 个答案:

没有答案