Получите данные внутри массива запросов внутри схемы

#node.js #mongodb #mongoose

Вопрос:

Как я могу создать маршрут для получения отправленных данных, которые находятся внутри массива запросов. Массив запросов находится внутри схемы Post

 const mongoose = require("mongoose") const {ObjectId} = mongoose.Schema.Types  const postSchema = new mongoose.Schema({  name:{  type: String,  required: true  },  jobtitle:{  type: String,  required: true  },  requests:[{  text: String,  postedby:{type: ObjectId, ref: "User"}  }],  postedby:{  type: ObjectId,  ref: "User"  } })  mongoose.model("Post", postSchema)   

Я попробовал это сделать, но получил еще одно сообщение от

 router.get("/acceptedpost",requireLogin,(req,res)=gt;{  Post.find({postedby:req.user._id})  .populate("postedby","_id name")  .then(accpost =gt; {  res.json({accpost})  })  .catch(err =gt; {  console.log(err)  }) })   

Ответ №1:

Если вы хотите сопоставить req.user._id данные с requests массивом, вы можете просто сделать:

 Post.find({"requests.postedby": req.user._id})  .populate("postedby", "requests.postedby")  .then(accpost =gt; {  res.json({accpost})  })  .catch(err =gt; {  console.log(err)  })