【问题标题】:Schema and Model are ok and Mongoose .populate not working架构和模型都可以,Mongoose .populate 不起作用
【发布时间】:2018-02-06 07:59:51
【问题描述】:

我从终端检查了我的数据库,数据正常,但是当填充“commnets”时仍然给我 ID

这是我展示数据的途径

var camp_id= req.params.id ;
Campground.findById(camp_id).populate("commnets").exec(function(err,fcg){
    if(err) { console.log(err) ;}
    else {
        console.log("No err") ;
        console.log(fcg) ;
        res.render("show",{campground: fcg});}


}) ;

moongose 模式(露营地和评论)

var campgroundSchema = new mongoose.Schema( {
name: String ,
image: String ,
description : String ,
comments: [
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Commnet"
    }
 ]
 } ) ;

var Campground = mongoose.model("Campground",campgroundSchema);

var commentSchema = new mongoose.Schema({
text: String ,
aurthor: String
}) ;

var Comment = mongoose.model("Commnet",commentSchema) ;

【问题讨论】:

    标签: node.js mongodb mongoose-populate


    【解决方案1】:

    我认为是因为拼写错误。您需要使用“cmets”而不是“commnets”

    var camp_id= req.params.id ;
    
    Campground.findById(camp_id).populate("comments").exec(function(err,fcg){
        if(err) { console.log(err) ;}
        else {
            console.log("No err") ;
            console.log(fcg) ;
            res.render("show",{campground: fcg});}
    
    
    }) ; 
    

    【讨论】:

    • 这很尴尬 :P 我在这上面浪费了很多时间 非常感谢 ^_^
    猜你喜欢
    • 2021-05-17
    • 2014-12-01
    • 2021-04-15
    • 2018-07-06
    • 2016-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    相关资源
    最近更新 更多