mongoose-subpopulate icon indicating copy to clipboard operation
mongoose-subpopulate copied to clipboard

how to use

Open ralyodio opened this issue 12 years ago • 8 comments

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.

ralyodio avatar Oct 26 '12 08:10 ralyodio

Did you try working with my existing example?

JoshuaGross avatar Oct 27 '12 07:10 JoshuaGross

yes, i couldn't quite understand what it was doing. or what is required to get it to work.

ralyodio avatar Oct 27 '12 07:10 ralyodio

If you have a specific question or post the code you tried to use, I'll be more able to help you.

JoshuaGross avatar Oct 30 '12 19:10 JoshuaGross

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 avatar Nov 12 '12 23:11 brunocasado

@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 avatar Nov 13 '12 08:11 JoshuaGross

@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?

brunocasado avatar Nov 13 '12 12:11 brunocasado

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?

chtenb avatar Dec 01 '12 10:12 chtenb

An example for us express users would be great too. Trying to figure out where to put everything is proving difficult.

aaroncaito avatar Dec 14 '12 16:12 aaroncaito