【问题标题】:Mongoose Populate Empty ArrayMongoose 填充空数组
【发布时间】:2020-01-09 00:21:37
【问题描述】:
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 个收藏以及制作主要收藏的收藏。也可以向我推荐一些好的网站,在那里我可以了解更多关于 mongoose join 的信息。

【问题讨论】:

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


    【解决方案1】:

    Cook 替换为 cook

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

    【讨论】:

      猜你喜欢
      • 2019-02-13
      • 2016-12-04
      • 2019-01-31
      • 2014-12-05
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      相关资源
      最近更新 更多