# Display a belongsToMany child in a parent record

**URL:** <https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097>\
**Category:** Help me!\
**Tags:** howtos\
**Created:** [August 13, 2021, 10:13am UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097 "2021-08-13T10:13:28Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadiab](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/nadiab/32/1812_2.png) [@nadiab](https://community.forestadmin.com/u/nadiab)\
**Post date:** [August 13, 2021, 10:13am UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/1 "2021-08-13T10:13:28Z")

</div>

Hello guys,

Hope you’re doing well.  
I’m contacting you because I have problems retrieving an attached entity.  
The relationship between theses entities is “belongsToMany” as following :

```javascript
    Documents.belongsToMany(models.refundRequests, {
      through: 'refundRequestDocument',
      foreignKey: 'documentId',
      otherKey: 'refundRequestId',
      as: 'refundRequestsThroughRefundRequestDocuments',
    });

    RefundRequests.belongsToMany(models.documents, {
      through: 'refundRequestDocument',
      foreignKey: 'refundRequestId',
      otherKey: 'documentId',
      as: 'documentsThroughRefundRequestDocuments',
    });

```

I want to display the refund request documents in the refund request page (record = refund request).  
I tried to return `record.documentsThroughRefundRequestDocuments` & `record.refundRequestDocument` but the result is always `undefined`.

I’ve noticed that I can display the related data `record.documentsThroughRefundRequestDocuments` but there is to many info, & I want to display the documents in a specific way.

```javascript
 "express": "^4.17.1",
 "express-jwt": "^5.3.3",
 "forest-express-sequelize": "^7.0.0",
 "sequelize": "~5.15.1",
 "pg": "~8.2.2",

```

Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [August 13, 2021, 12:55pm UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/2 "2021-08-13T12:55:52Z")

</div>

Hi @nadiab,

What are your trying to achieve, I mean you say you tried `record.documentsThroughRefundRequestDocuments` but where are you trying that ? In a Smart View ? Or on your back end ?

---

<div class="post-metadata">

**Author:** ![nadiab](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/nadiab/32/1812_2.png) [@nadiab](https://community.forestadmin.com/u/nadiab)\
**Post date:** [August 13, 2021, 1:05pm UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/3 "2021-08-13T13:05:01Z")

</div>

Hi @anon62739609,

Thanks for reaching out.  
I’m trying to retrieve the attached documents.  
I firstly tried `record.documents` but the result is undefined.  
Is there a way to “include” the `document` without re-writing the request in the forest refund request view ?

---

<div class="post-metadata">

**Author:** ![nadiab](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/nadiab/32/1812_2.png) [@nadiab](https://community.forestadmin.com/u/nadiab)\
**Post date:** [August 13, 2021, 3:24pm UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/4 "2021-08-13T15:24:05Z")

</div>

I rewrote the “get” request including documents but nothing is returned :

```javascript
        const refundRequest = await models.refundRequests.findByPk(record.id, {
          where: {
            id: record.id
          }, 
          include: [{
             model: models.documents
          }]
        });

```

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [August 16, 2021, 8:01am UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/5 "2021-08-16T08:01:59Z")

</div>

Hey @nadiab,

Sorry but you did not answered my question 😅 . Are you doing a smart view ? I’m missing some context 😉

---

<div class="post-metadata">

**Author:** ![nadiab](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/nadiab/32/1812_2.png) [@nadiab](https://community.forestadmin.com/u/nadiab)\
**Post date:** [August 16, 2021, 8:05am UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/6 "2021-08-16T08:05:03Z")

</div>

Hello @anon62739609,  
Sorry, I thought it was explicit.  
Yes i’m doing a smart view (refund request view).

---

<div class="post-metadata">

**Author:** ![nadiab](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/nadiab/32/1812_2.png) [@nadiab](https://community.forestadmin.com/u/nadiab)\
**Post date:** [August 16, 2021, 9:46am UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/7 "2021-08-16T09:46:02Z")

</div>

![Screenshot 2021-08-16 at 10.45.11](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/f/f87a6348b31a39aa275d6f990a3f5bdc9091c8af.png)  
I have the documents on the related data section but I can’t retrieve it from the record nor via a sequelize request (include) & display it in my own way.

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [August 16, 2021, 12:29pm UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/8 "2021-08-16T12:29:40Z")

</div>

It’s because your need to prefix always your attributes/relationships with `forest-`.  
So in your case you need to do `record.forest-documentsThroughRefundRequestDocuments`. Use the autocompletion on the Smart View Editor it will help you (Ctrl + Space or on Mac it’s Cmd + Space)

---

<div class="post-metadata">

**Author:** ![nadiab](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/nadiab/32/1812_2.png) [@nadiab](https://community.forestadmin.com/u/nadiab)\
**Post date:** [August 16, 2021, 12:44pm UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/9 "2021-08-16T12:44:41Z")

</div>

Unfortunately it doesn’t work 😕

> Cannot set the documents value because of an unexpected error: documentsThroughRefundRequestDocuments is not defined

Without the “forest-” I have `undefined` when I print the value.

---

<div class="post-metadata">

**Author:** ![nadiab](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/nadiab/32/1812_2.png) [@nadiab](https://community.forestadmin.com/u/nadiab)\
**Post date:** [August 16, 2021, 1:05pm UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/10 "2021-08-16T13:05:45Z")

</div>

Sorry I meant summary view instead of smart view.  
I created a section to display refund request documents.

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [August 16, 2021, 5:11pm UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/11 "2021-08-16T17:11:48Z")

</div>

Can you please share a screen of where you have the error with the web console open please 🙏 ?

---

<div class="post-metadata">

**Author:** ![nadiab](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/nadiab/32/1812_2.png) [@nadiab](https://community.forestadmin.com/u/nadiab)\
**Post date:** [August 17, 2021, 9:46am UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/12 "2021-08-17T09:46:20Z")

</div>

@anon62739609 yes of course :

> **[Screenshot-2021-08-17-at-10-07-26](https://ibb.co/MC34Btq)**
>
> Image Screenshot-2021-08-17-at-10-07-26 hosted in ImgBB

> **[Screenshot-2021-08-17-at-10-09-50](https://ibb.co/fQDDxk7)**
>
> Image Screenshot-2021-08-17-at-10-09-50 hosted in ImgBB

> **[Screenshot-2021-08-17-at-10-36-47](https://ibb.co/GRtLKpZ)**
>
> Image Screenshot-2021-08-17-at-10-36-47 hosted in ImgBB

> **[Screenshot-2021-08-17-at-10-38-50](https://ibb.co/2YCwnDC)**
>
> Image Screenshot-2021-08-17-at-10-38-50 hosted in ImgBB

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [August 17, 2021, 12:58pm UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/13 "2021-08-17T12:58:31Z")

</div>

Okey your issue is pretty simple then 😄, you just need to do:

```javascript
const refundRequests = await models.refundRequests.findByPk(record.id, {
  include: [{
    model: models.documents,
    as: 'documentsThroughRefundRequestDocuments',
  }],
});

```

You were missing the `as` part. When you declare a relationship with an `as` you **MUST** specify it on every request on that relationship 😉.

Another easier solution is just doing:

```javascript
const documents = await record.getDocumentsThroughRefundRequestDocuments();

```

---

<div class="post-metadata">

**Author:** ![nadiab](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/nadiab/32/1812_2.png) [@nadiab](https://community.forestadmin.com/u/nadiab)\
**Post date:** [August 17, 2021, 2:31pm UTC](https://community.forestadmin.com/t/display-a-belongstomany-child-in-a-parent-record/3097/14 "2021-08-17T14:31:10Z")

</div>

Thanks for your help it works !  
Yes the 2nd solution is better.  
Thanks again.  
Have a nice day.
