首页 > 解决方案 > 猫鼬种群不存在

问题描述

人口没有在下面的代码中工作..我该怎么办?我想用他的参展商填充这个“摊位”模型。(参展商编号)。

这是模型

const stall = new mongoose.Schema({
    name:{
        type:String,
        default:'Stall Name'
    },
    description:{
        type:String,
        default:'Stall Description'
    },
    logoImage:{
        type:String,
        default:'/public/images/logoImage.jpg'
    },
    exhibitorID:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Exhibitor'
    },
    eventID:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Event'
    }
})

module.exports = mongoose.model('Stall',stall)

这是路线

router.get('/all/:eventID',eventAdminAuth,async(req,res)=>{
    try{
        var ob = await event.findById(req.params.eventID)
        var stalls = await stall.find({eventID:req.params.eventID}).populate('Exhibitor')
        console.log(stalls)
        if(ob){
            res.locals.event=ob
            res.render('eventSettings/stalls',{stalls:stalls})
        }else{
            res.send('no event')
        }
    }catch(err){
        res.send(err)
    }
})

标签: javascriptnode.jsexpressmongoose

解决方案


您的档位查询应如下所示:

var stalls = await stall.find({eventID:req.params.eventID}).populate('exhibitorID')
console.log(stalls)

由于您已经使用条目“exhibitorID”引用了“Exhbibitor”,因此您应该在 .polupate() 函数的论证中使用该字段。


推荐阅读