如何使用Express和Mongodb将当前用户作为作者添加到博客

如何使用Express和Mongodb将当前用户作为作者添加到博客,express,mongoose,jwt,objectid,Express,Mongoose,Jwt,Objectid,我正在使用Express、MongoDB和JWT进行身份验证。在有效身份验证时,我将当前用户ID附加到req对象 我正在添加作为博客作者的当前用户ID 以下是博客和用户模型 const BlogSchema = new mongoose.Schema({ title: {type: String, required: true}, body: {type: String, required: true}, author : {type:mongoose.Schema.Types

我正在使用Express、MongoDB和JWT进行身份验证。在有效身份验证时,我将当前用户ID附加到req对象

我正在添加作为博客作者的当前用户ID

以下是博客和用户模型

const BlogSchema = new mongoose.Schema({
   title: {type: String, required: true},
   body: {type: String, required: true},
   author : {type:mongoose.Schema.Types.ObjectId, ref: "UserSchema"}
  })
module.exports = mongoose.model("Blog", BlogSchema);


const UserSchema= new mongoose.Schema({
   name: {type: String, required: true},
   email: {type: String, required: true},
   blogs : [{type:mongoose.Schema.Types.ObjectId, ref: "BlogSchema"}]
 })
module.exports = mongoose.model("Blog", UserSchema);
我正在添加从JWT验证中获得的当前用户,并将其添加到博客模型实例中。这是可行的,但是博客ID没有被添加到用户的博客数组中


如何避开它?请帮忙

你能发布你收到的回复吗?嘿,事实上我自己解决了。我所做的只是在将新博客推送到用户的博客[]之后,我使用了user.save()