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.

getAll request on '_MANY' association

See original GitHub issue

Describe the bug I want to get ‘_MANY’ association and rest-hapi return all datas of the model of association

To Reproduce Steps to reproduce the behavior:

  1. Create ‘_MANY’ association
associations: {
	companies: {
		type: '_MANY',
		alias: 'companies',
		model: 'companies',
	},
},
  1. I am creating companies in the model companies Capture d’écran 2019-08-22 à 18 07 04

  2. I post an id company in my companies association Capture d’écran 2019-08-22 à 18 19 53

  3. I get the association companies and response contains all the companies whereas in the companies association i have only one company

{
  "docs": [
    {
      "_id": "5d5cae2e3356752c4f2d4414",
      "subsidiaries": [
        {
          "_id": "5d5cae373356752c4f2d4416",
          "subsidiaries": [],
          "name": "string",
          "naf": "string",
          "siret": "string",
          "createdBy": "5d5c9e1a6b233e246ad4f611",
          "createdAt": "2019-08-21T02:36:39.450Z",
          "institutions": []
        }
      ],
      "name": "string",
      "naf": "string",
      "siret": "string",
      "createdBy": "5d5c9e1a6b233e246ad4f611",
      "createdAt": "2019-08-21T02:36:30.548Z",
      "institutions": []
    },
    {
      "_id": "5d5cae373356752c4f2d4416",
      "subsidiaries": [],
      "name": "string",
      "naf": "string",
      "siret": "string",
      "createdBy": "5d5c9e1a6b233e246ad4f611",
      "createdAt": "2019-08-21T02:36:39.450Z",
      "institutions": []
    },
    {
      "_id": "5d5ebd0207b9b804f763dc2a",
      "subsidiaries": [],
      "name": "Hey",
      "naf": "string",
      "siret": "string",
      "createdBy": "5d5db71b1c99220acc90ae45",
      "createdAt": "2019-08-22T16:04:18.189Z",
      "institutions": []
    }
  ],
  "pages": {
    "current": 1,
    "prev": 0,
    "hasPrev": false,
    "next": 2,
    "hasNext": false,
    "total": null
  },
  "items": {
    "begin": null,
    "end": null,
    "total": 3
  }
}

Expected behavior getAll request on ‘_MANY’ association should return only the id’s of the association and not fulld datas of the corresponding model.

Desktop (please complete the following information):

  • OS: OSX
  • Browser: Chrome
  • Version: 76.0.3809.100

Additional context Sorry for my deplorable English, i hope its comprehensive ! Thanks for your answer.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
JKHeadleycommented, Aug 24, 2019

Hi @Clemssss, it’s because you are overriding the query parameter in the middleware function instead of modifying it. getAll internally uses the query parameter to filter the results based on the ownerId. When you override it, you erase the filter and therefore get all of the company documents rather than the docs under the ownerId.

Try changing your middleware to:

	find: {
	  pre: (_id, query, request, Log) => ({
            ...query,
            $embed: ['subsidiaries']
          }),
	},
	list: {
	  pre: (query, request, Log) => ({
            ...query,
            $embed: ['subsidiaries']
          }),
	},
1reaction
JKHeadleycommented, Aug 25, 2019

Not at all, its easy to miss!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get all children or parents in a many ... - Stack Overflow
You need to set up the reverse association as well. Person.hasMany(Person, { as: 'Children', through: models.Person_Parent });.
Read more >
Solved: HubSpot Community - Get all associations API
Solved: My immeidate challenge has to do with getting line items associated with a deal however according to this post ( which is...
Read more >
Sequelize Many-to-Many Association example - Node.js ...
Way to implement Sequelize Associations example: many to many Relationship in Node.js - Sequelize Associate tutorial with example.
Read more >
Advanced M:N Associations | Sequelize
By passing a string to through above, we are asking Sequelize to automatically generate a model named User_Profiles as the through table ...
Read more >
MATCH - Cypher Manual - Neo4j
The MATCH clause allows you to specify the patterns Neo4j will search for in the database. This is the primary way of getting...
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