猫鼬填充空数组

时间:2019-11-05 05:34:58

标签: node.js mongoose mongoose-schema mongoose-populate

const Schema = mongoose.Schema    
const logEntry = new Schema({
    no:{
        type: Number
    },
    type: {
        type: String
    },
    createdAt: {
        type: Date,
        default: Date.new  
    },
    ingredients: {
        type: String
    },
    process: {
        type: String
    },
    cook: [{
        type: mongoose.Schema.Types.ObjectId, ref: 'shef'
    }]
});

const shefEntry = new Schema({
    name: {
        type: String
    },
    dish: {
        type: mongoose.Schema.Types.ObjectId, ref: 'logDetails'
    }
})

const foodItem = mongoose.model('logDetails', logEntry);
const Cook = mongoose.model('shef', shefEntry);

这是我的架构。

app.route('/log_entries')
    .get(controller.index)
    .post(controller.create)

这是我的路线。

    exports.index = function(req, res){
    logEntry.find({})
    .populate('Cook')
    .exec(function(err, logEntry){
        if(err) res.send(err);
            res.json(logEntry);
    })
}

这是我要填充的控制器。我的要求是将厨师的名字填入食谱集中,但得到的结果为

{
    "cook": [],
    "_id": "5dafdbd8b9cefa2670ab73c7",
    "no": 101,
    "type": "Non-Veg",
    "ingredients": "Gobi,Paneer",
    "process": "Go on with the manual",
    "__v": 0
},
{
    "cook": [],
    "_id": "5dbb76bf5356143124d3afbd",
    "no": 101,
    "type": "veg",
    "ingredients": "Gobi,Paneer",
    "process": "Go on with the manual",
    "__v": 0
}

有人可以解释如何使用猫鼬吗?我的意思是关于如何加入2个收藏集以及哪个收藏集成为主要收藏集。还可以通过一些不错的网站向我推荐,我可以在其中了解有关猫鼬加盟的更多信息。

1 个答案:

答案 0 :(得分:0)

烹饪替换为烹饪

  exports.index = function(req, res){
        logEntry.find({})
        .populate('cook')
        .exec(function(err, logEntry){
            if(err) res.send(err);
                res.json(logEntry);
        }) }