mongoose-subpopulate
mongoose-subpopulate copied to clipboard
how to use
I don't understand how to use your module.
can you provide a clean example?
I installed with npm, and tried .populate('list.user') but it didn't do anything.
Did you try working with my existing example?
yes, i couldn't quite understand what it was doing. or what is required to get it to work.
If you have a specific question or post the code you tried to use, I'll be more able to help you.
Good Night Joshua,
I define my models like this:
var ProjectCommentSchema = new Schema({ text: { type: String/*, required: true */ }, _user: { type: Schema.ObjectId, ref: 'User', required: true}, _idea: { type: Schema.ObjectId, ref: 'Project', required: true}, _reply: [{type: Schema.ObjectId, ref: 'ProjectComment', required: false }], posted: { type: Date, required: true, 'default': Date.now }, votes_up: { type: Number, 'default': 0 }, votes_down: { type: Number, 'default': 0 } });
var ProjectComment = mongoose.model('ProjectComment', ProjectCommentSchema);
now i add this:
ProjectComment = mongooseSubpopulate.wrapSchema(mongoose.model('ProjectComment')); mongooseSubpopulate.extendMongoose(mongoose, ProjectComment );
I not have a function of defineModels and my exports are thus:
exports.ProjectComment = ProjectComment;
but when i run a query:
models.ProjectComment.find().populate('_reply').populate('_reply._reply').exec(function(err,data){ console.log(data); })
i get this message in console:
Schema hasn't been registered for model "undefined".
Where's problem?
i not use function defineModels and i do exports one by one
@brunocasado - extendMongoose is being used incorrectly. Call it first. Additionally, the second argument is a callback function; that should be defineModels. You do not have to use defineModels exactly as I do, but I recommend that you set up schemas from within defineModels (either directly or by calling functions that set up those schemas).
Additionally, has the 'Project' schema been modeled?
@JoshuaGross yes, was modeled.
Bad the problem the entire project is well advanced and was not used to function defineModels and I think this could be a problem.
We are declaring the schemas and compiling models without using functions, is there any form of using its complement in some way different from yours?
This is an issue for me as well. My project is well advanced, and I recently discovered that I'm in need of nested population. However, I couldnt get the example to work either. Is there any way of integrating this subpopulate stuff into an existing project with a structure similar to the examples in the mongoose docs?
An example for us express users would be great too. Trying to figure out where to put everything is proving difficult.