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.

Associations within subdocs?

See original GitHub issue

(This is more of a question than an issue, but it could turn into an issue.)

Is it possible, in rest-hapi, to have an association within a subdocument?

For example, I can do this in Mongoose, where the association is at subdoc.pets, instead of being at the top level of the document:

const ownerSchema = new Schema({
  name: Schema.Types.String,
  subdoc: {
    pets: [{ type: Schema.Types.ObjectId, ref: 'pet' }],
  },
});

Owner.findOne().populate('subdoc.pets');

If it were at the top level, I would implement this in rest-hapi by adding an association under routeOptions. I’m not sure how or if I can do this within a subdocument, however.

I don’t actually need endpoints for this particular association; I just need to be able to populate it when requesting the document, if that makes a difference.

Thanks!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
fabripecocommented, Apr 19, 2019

Populating subdocs would be a very important feature for an association. From the Swagger documentation it seems to be possible image

but server returns 500 with this error — Error: Association not found. [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at nestPopulate (/home/fabrizio/Projects/gse-bip/gse-server/backend/node_modules/rest-hapi/utilities/query-helper.js:523:13) [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at /home/fabrizio/Projects/gse-bip/gse-server/backend/node_modules/rest-hapi/utilities/query-helper.js:408:20 [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at Array.forEach (<anonymous>) [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at Object.populateEmbeddedDocs (/home/fabrizio/Projects/gse-bip/gse-server/backend/node_modules/rest-hapi/utilities/query-helper.js:404:20) [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at Object.createMongooseQuery (/home/fabrizio/Projects/gse-bip/gse-server/backend/node_modules/rest-hapi/utilities/query-helper.js:88:23) [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at _listHandler (/home/fabrizio/Projects/gse-bip/gse-server/backend/node_modules/rest-hapi/utilities/handler-helper.js:115:33)

It is a Mongoose feature, why is it not possibile with RestHapi?. Or am I wrong in something? Thanks!

1reaction
JKHeadleycommented, Apr 18, 2018

@tamias thanks for pointing this out. I agree! I think I’ll add two more feature requests: one to have an option of disabling .lean() and one for populating subdocs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose 101: An Introduction to the Basics, Subdocuments ...
In this article, we'll go through: The basics of using Mongoose; Mongoose subdocuments; Mongoose population. By the end of the article, you ...
Read more >
Mongoose Associations - Rithm School
The idea with embedded documents (also called "subdocuments") is that we place data we want to associate inside of an existing document.
Read more >
Model One-to-Many Relationships with Embedded Documents
This page describes a data model that uses embedded documents to describe a one-to-many relationship between connected data. Embedding connected data in a ......
Read more >
How to get subdocuments of multiple documents in mongoose
I have a Module called Organisation with an array calls users in it that contains UserSchema objects. Now i need a query to...
Read more >
Getting Started with Subdocuments in Azure Cosmos DB
This article will show you how to start working with Subdocuments in Azure Cosmos DB and how easilly start working with them.
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