Using Mongoose Dynamic Populate with unexpected result

110 views
Skip to first unread message

samuel cicero

unread,
Aug 3, 2017, 9:52:50 AM8/3/17
to mongoose-users

Currently have a concept of a Slideshow and different types of slides. I'm attempting to populate a slides array on a slideshow object with a dynamic referance set as the slide type to get a populated array of slides with different documents.


Matching the docs at http://mongoosejs.com/docs/populate.html under the title "Dynamic References" my code looks like this.


// Abreviated slideshow model
var slideshowSchema = mongoose.Schema({
    slides: [{
        slideType: String,
        item: {
            type: Schema.Types.ObjectId,
            refPath: 'slides.slideType'
        }
    }]
}]


// controller get route
app.get('/slideshow/:id', function(req, res, next) {
    var id = req.params.id;
    if(id){
        Slideshow
            .findById(id)
            .populate('slides.item')
            .exec(function(err, slideshow) {
                res.writeHead( 200, { "Content-Type": "application/json" } );
                res.end(JSON.stringify(slideshow));
            });
    }
    else{
        return next();
    }
});


// type of slide trying to be populated
var currentWeatherSchema = mongoose.Schema({

  slideType: {
      type: String,
      required: true
  },

  slideshowId: {
      type: Schema.Types.ObjectId,
      required: true
  }

  // other unique props

});


When doing a direct populate on a hardcoded slideType, the slides populate as expected, and when running the above code i do get a response, and the slides array is populated with the correct amount of objects, but they look like this instead of the expected document:


"slides": [
    {
        "_bsontype": "ObjectID",
        "id": {
            "type": "Buffer",
            "data": [
                89,
                126,
                152,
                150,
                243,
                157,
                179,
                147,
                165,
                23,
                247,
                56
            ]
        }
    }
]


I've read through the examples countless times, and cannot figure out where I'm going wrong. Does anyone have any experience doing this, or see what i may be missing?

Reply all
Reply to author
Forward
0 new messages