尝试一次将新帖子添加到数据库中,一次添加,第二次则提示错误 发布架构如下
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})