moongose subschema无法正常工作

时间:2016-09-13 03:38:16

标签: node.js express mongoose mongoose-schema

var commentSchema = new Schema({
 rating:  {
    type: Number,
    min: 1,
    max: 5,
    required: true
 },
 comment:  {
    type: String,
    required: true
 },
 postedBy: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    index:true
 }
 },{
   timestamps: true
 });

 var postSchema = new Schema({
  title:{
    type:String,
    required:true
   },
  image:{
   type:String,
   required:true
  },
  meta:{
   type:String,
   required:true
  },
  story:{
   type:String,
   required:true
  },
 likes:{
   type:Number,
   min:0,
   required:true
  },
  postedBy: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    index:true
   },
 comments:[commentSchema]
},
{
 timestamps: true
 });

这是我的架构设计

.get(function(req,res,next){
  Posts.find({})
  .populate('postedBy')
  .populate('comments.postedBy')
  .exec(function (err, post) {
    if (err) throw err;
    res.json(post);
});

用户架构

var User = new Schema({
username: String,
password: String,
firstname: {
  type: String,
    default: ''
},
lastname: {
  type: String,
    default: ''
},
biodata: {
  type: String,
  default: ''
},
admin:{
    type: Boolean,
    default: false
}
});

但是我的mongoose架构还没有工作,输出显示没有任何postBy字段无法正常工作

{
"_id": "57d76b26a404a9072c43db0e",
"updatedAt": "2016-09-13T03:13:10.901Z",
"createdAt": "2016-09-13T02:57:42.894Z",
"title": "Long time no shot!",
"image": "https://d13yacurqjgara.cloudfront.net/users/501822/screenshots/2946804/hot-shot.png",
"meta": "This time, I'm exploring a UI for a promo app. This still early exploration, so any feedback will be welcome :)",
"story": "Our last shot got a lot of love and questions regarding this project we were never even going to show and with that encouragement we felt it would be cool to show the entire spectrum of this 2016 Annual Report we just wrapped up for Leading the Way Ministry.The report is a full 28 page spread that has a specific type and color system but also a unique system for textures and brushes. This is the first time we have set a restriction on what type of texturing goes on specific elements within a design, especially one of this size. This was a bit of a game changer for us and something we will be exploring more of in the future.TO GET THIS LOOK: Once we had each spread completed we laid out all of the final JPG images on a very large canvas. Once we had the spreads arranged how we wanted we then used our new GRANITE Photoshop Action Set to get that faded / dramatic black and white feel to cover all of the images. We then applied very subtle texture and noise on top of that then isolated some of the vector badges and type elements, enlarged them, gave each a loud orange color and set them to Lighten.Thank you to everyone for your kind words and the encouragement!",
"likes": 300,
"__v": 3,
"comments": [
  {
    "updatedAt": "2016-09-13T03:04:27.408Z",
    "createdAt": "2016-09-13T03:04:27.408Z",
    "rating": 4,
    "comment": "test comment",
    "_id": "57d76cbb54ce630ad8e3922f"
  },
  {
    "updatedAt": "2016-09-13T03:06:10.891Z",
    "createdAt": "2016-09-13T03:06:10.891Z",
    "rating": 2,
    "comment": "test comment",
    "_id": "57d76d22d9be810530c26c25"
  },
  {
    "updatedAt": "2016-09-13T03:13:10.900Z",
    "createdAt": "2016-09-13T03:13:10.900Z",
    "rating": 2,
    "comment": "test comment",
    "_id": "57d76ec6c4eba41e742e1bbf"
  }
]
}

任何人都可以告诉我服务器上的问题在哪里显示没有错误。

0 个答案:

没有答案