Display a belongsToMany child in a parent record

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 :

    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.

 "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.

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 ?

Hi @vince,

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 ?

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

        const refundRequest = await models.refundRequests.findByPk(record.id, {
          where: {
            id: record.id
          include: [{
             model: models.documents

Hey @nadiab,

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

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

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.

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)

Unfortunately it doesn’t work :confused:

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.

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

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

@vince yes of course :

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

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 :wink:.

Another easier solution is just doing:

const documents = await record.getDocumentsThroughRefundRequestDocuments();

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

1 Like