Schema and Model are ok and Mongoose .populate not working

164 Views Asked by At

I checked my db form the terminal and the data is ok but when populate 'commnets' still give me the ids

this is my route to show the data

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 schemas (Campground and Comment )

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) ;
1

There are 1 best solutions below

1
On BEST ANSWER

I think it is because of spelling mistake . you need to use "comments" instead of "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});}


}) ;