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.

Populating multiple fields with a pre "find" middleware

See original GitHub issue

Hello,

I am trying to specify the fields that should always be populated for the user document in a pre “find” middleware, like this: userSchema.pre(/^find/, function (next) { this.populate("followers following"); next(); });

But when I send the request it’s just stuck not sending any response.

It works just fine when I only populate one field, either ‘followers’ or ‘following’, but together it won’t work. I tried a bunch of different ways, but nothing seems to work.

I’ve also created a stack overflow question and none of the ideas work. In the mongoose documentation there is only an example of one field being populated, and populating multiple fields really shouldn’t be harder than it is with a normal query populate method, but it is.

If there is really no way to populate multiple fields, this is a huge issue to be fair.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Dec 29, 2021

The issue is that your populate() call triggers infinite recursion: this.populate('followers following') triggers the same middleware for both followers and following.

As a workaround, add a _recursed option so your middleware knows to avoid populating recursively:

  userSchema.pre(/^find/, function (next) {
    if (this.options._recursed) {
      return next();
    }
    this.populate({ path: "followers following", options: { _recursed: true } });
    next();
  });
0reactions
aleksadjukiccommented, Dec 19, 2021

Nope… The request just gets stuck, and I don’t get a response…

When I do just this.populate('followers') or just this.populate('following'), they both work fine alone, but if I try to populate both fields it just doesn’t work, I’ve tried a bunch of things…

It does work when you do it on the model query, but we should definitely be able to do it in the middleware as well…

I really hope I’m making a mistake somewhere, but I think that unfortunately that’s not the case and that this is a bug…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Populating multiple fields with a pre "find" middleware
I am trying to specify the fields that should always be populated for the user document in a pre "find" middleware, like this:...
Read more >
Mongoose v6.8.2: Query Population
Populate in Middleware​​ You can populate in either pre or post hooks. If you want to always populate a certain field, check out...
Read more >
When ever i m populating field in pre middleware so i m got ...
The result i got i have search in that alot but not getting any this field. getAllMoney Query { _mongooseOptions: { populate: {...
Read more >
How to use Populate in Mongoose & Node.js
What is Population ?? · If no document is found to populate, then field will be null . · In case of array...
Read more >
Complete guide for Typescript with Mongoose for Node.js
It seems all the other blog posts or tutorials we can find online… ... Typing for query middleware with _update field.
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