Discussion:
Using Mongoose Dynamic Populate with unexpected result
samuel cicero
2017-08-03 13:52:50 UTC
Permalink
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 modelvar 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 populatedvar 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?
--
You received this message because you are subscribed to the Google Groups "mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-users+***@googlegroups.com.
To post to this group, send email to mongoose-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.
Loading...