question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot populate nested parent model

See original GitHub issue

Hello,

I am trying to use mongoose-deep-populate, but got this error

Error: Plugin was not installed
    at Query.deepPopulate (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/mongoose-deep-populate/lib/plugin.js:16:61)
    at Object.exports.show [as handle] (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/server/api/user/user.controller.js:45:5)
    at next_layer (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/route.js:103:13)
    at Route.dispatch (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/route.js:107:5)
    at c (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/index.js:195:24)
    at param (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/index.js:268:14)
    at param (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/index.js:280:16)
    at Function.proto.process_params (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/index.js:296:3)
    at next (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/index.js:189:19)
    at next (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/index.js:166:38)
    at next (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/index.js:166:38)
    at next_layer (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/route.js:77:14)
    at next_layer (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/route.js:81:14)
    at next_layer (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/route.js:81:14)
    at Route.dispatch (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/route.js:107:5)
    at c (/Users/christianmsakai/Desktop/Baby Mart/ninthbeat/node_modules/express/lib/router/index.js:195:24)

My code is

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var crypto = require('crypto');
var _ = require('lodash');
var extend = require('mongoose-schema-extend');
var deepPopulate = require('mongoose-deep-populate');
var authTypes = ['facebook', 'google', 'twitter'];

// User Schema
var UserSchema = new Schema({
  userName: { type: String, trim: true },
  items: [{ type: Schema.Types.ObjectId, ref: 'Item' }],
});

/**
 * Plugins
 */
UserSchema.plugin(deepPopulate);
exports.show = function (req, res, next) {
  var userId = req.params.id;

  User
    .findById(userId)
    .deepPopulate('items.seller')
    .exec(function (err, user) {
    if (err) return next(err);
    if (!user) return res.send(401);
    res.json(user.profile);
  });
};
'use strict';

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var ItemSchema = new Schema({
    seller: { type: Schema.Types.ObjectId, ref: 'User' },
    type: String,
  title:  { type: String, trim: true },
    condition: String,
  description: { type: String, trim: true },
  price: Number,
  imageUrls: [String],
  userLikes: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  createdAt: { type: Date, default: Date.now }
});

module.exports = mongoose.model('Item', ItemSchema);

Any ideas?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:20 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ansgarmcommented, Oct 1, 2015

Had this issue myself. The deepPopulate plugin integrates itself into the methods of the Model. Hence calling it after

userSchema.methods = {
 ...
};

will solve your problem. If you call it first, the deepPopulate method will get overwritten…

0reactions
backend-stunntechcommented, Jun 28, 2018

Thanks, @ansgarm, you helped me solve the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to infinitely populate a mongoDB model? - Stack Overflow
I am able to nest two populates but this commenting system is similar to Reddit in that there can be an infinite number...
Read more >
sort of populate with nested populate not working · Issue #7072
I try to rename the sort field in parent and firstChild because there are the same name, but the result is the same....
Read more >
Mongoose v6.8.2: Query Population
Setting Populated Fields. You can manually populate a property by setting it to a document. The document must be an instance of the...
Read more >
Eager Loading - Sequelize
To obtain top-level WHERE clauses that involve nested columns, Sequelize provides a way to reference nested columns: the '$nested.column$' ...
Read more >
mongooseerror: cannot populate path `user` because it is not ...
Use nested populate as in the example below (The example assumes that a Token ... I've also examined the model.js source code and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found