无法填充mongoose架构。

时间:2017-08-22 01:30:20

标签: javascript node.js mongodb mongoose

我无法填充嵌套对象数组。我有点理解populate方法,但我不确定如何为这个api调用正确编写正确的代码,特别是填充嵌套对象。我对Node.Js或猫鼬没有多少经验,所以我感谢你给我的任何帮助或提示。

这是我的架构:

var mongoose = require('mongoose');

var PostsOfTheWeekSchema = new mongoose.Schema({
    sunday: {
        posts: [{}],
        date: Date
    },
    monday: {
        posts: [{}],
        date: Date
    },
    tuesday: {
        posts: [{}],
        date: Date
    },
    wednesday: {
        posts: [{}],
        date: Date
    },
    thursday: {
        posts: [{}],
        date: Date
    },
    friday: {
        posts: [{}],
        date: Date
    },
    saturday: {
        posts: [{}],
        date: Date
    },
});

module.exports = mongoose.model('PostsOfTheWeek', PostsOfTheWeekSchema);

这是我的api电话:

router.get('/postsOfTheDay', function (req, res, next) {
    PostsOfTheWeek.find({}).populate({path: 'posts', populate: {path: 'post', model: 'Post'}}).exec(function (err, posts) {
        if (err) { return next(err); }

        res.json(posts);
    });
});

以下是我对api电话的回复:

{
"_id": "599a5a529f5cee12bbeb7737",
"__v": 0,
"saturday": {
"posts": [],
},
"friday": {
"posts": [],
},
"thursday": {
"posts": [],
},
"wednesday": {
"posts": [],
},
"tuesday": {
"posts": [],
},
"monday": {
"posts": [],
},
"sunday": {
"date": "2017-08-21T03:58:10.000Z",
"posts": [
  {
"name": "Music",
"post": "599a298f5bdc6e107a8a5a48"
},
  {
"name": "News",
"post": "599a134189c3150fe701d384"
}
],
}
}

我似乎无法填充帖子属性。欢迎任何帮助或提示,感谢您的阅读。

0 个答案:

没有答案