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.

$populate does not seem to work

See original GitHub issue

Steps to reproduce

Create a linked relationship between two models

Expected behavior

When I query a basic endpoint I expect the $populate parameter to include my related model.

Actual behavior

The related model is not included in my results. The include never gets sent to sequelize and therefore the inner join is never made.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dafflcommented, Feb 14, 2017

Yes, you can do that by setting params.sequelize in a hook:

app.service('sequelizeService').before({
  find(hook) {
    hook.params.sequelize = {
      include: hook.params.query.$populate
    }
  }
});
0reactions
sarkistltcommented, Feb 28, 2018

@L3V147H4N it won’t work because ‘Role’ is string not a model, I’m using this hook:

function resolveModels(arr, models) {
  const include = [];

  arr.forEach((entity) => {
    if (typeof entity === 'string') {
      include.push(entity);
    } else if (typeof entity === 'object') {
      if (entity.model && typeof models[entity.model] === 'function') {
        entity.model = models[entity.model];
      }
      if (entity.include && Array.isArray(entity.include)) {
        entity.include = resolveModels(entity.include, models);
      }
      include.push(entity);
    }
  });

  return include;
}

export default function sequalizePopulateHook(ctx) {
  if (
    ctx.params.query &&
    ctx.params.query.$populate &&
    Array.isArray(ctx.params.query.$populate)
  ) {
    const { models } = ctx.app.get('sequelize');

    ctx.params.sequelize = ctx.params.sequelize || {};
    ctx.params.sequelize.include = resolveModels(ctx.params.query.$populate, models);
    ctx.params.sequelize.raw = false;

    delete ctx.params.query.$populate;
  }
  return ctx;
}

it will resolve model from string in:

$populate: [
  {
    model: 'Role', as: 'roles'
  }
]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose Populate not working - node.js - Stack Overflow
I wanted to populate a document after it was queried. This didn't work: // IIFE for async/await ( async() => { var user...
Read more >
Mongoose .populate() is not working with node.js app
First post here, so hello to everyone! I'm having trouble with mongoose populate because it's apparently doing nothing.
Read more >
Mongoose v6.8.2: Query Population
Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s). We may populate a single ......
Read more >
Populate doesnt work on mongoose
I've been trying to populate Auth with Voters Document but it doesnt seem to work. Sorry for the long a$$ code const voterSchema...
Read more >
Email TO field doesn't populate from address book
The autofill for some customers (primarily those on the west coast) has stopped working due to a bug within webmail. We are working...
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