# New Forest Admin agent Node.js - Adding relationships with other relationships that cause problems

**URL:** <https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448>\
**Category:** Help me!\
**Tags:** howtos, setup\
**Created:** [September 13, 2024, 8:20am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448 "2024-09-13T08:20:09Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [September 13, 2024, 8:20am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/1 "2024-09-13T08:20:09Z")

</div>

Hello 👋

## Feature(s) impacted

```javascript
collection.addOneToOneRelation()

collection.addOneToManyRelation()

```

## Observed behavior

⚠ **Important info: the relationships I add to my table can sometimes be null, i.e. there’s no data, this is deliberate.**

When I add relationships to my beneficiaries table, I have a display problem that only occurs in certain cases.

Here is my code, which you can scroll down to see:

```javascript
agent.customizeCollection('beneficiaries', collection => {
      // RELATIONS
      collection.addOneToOneRelation('np_contract', 'NP_contracts', {
        originKey: 'fk_beneficiaryId',
        originKeyTarget: 'id',
      });

      collection.addOneToOneRelation('np_subscription', 'NP_subscriptions', {
        originKey: 'fk_beneficiaryId',
        originKeyTarget: 'id',
      });
      
      collection.addOneToManyRelation('np_refundRequests', 'NP_refundRequests', {
        originKey: 'fk_beneficiaryId',
        originKeyTarget: 'id',
      });

      // collection.addOneToOneRelation('nv_contract', 'NV_contracts', {
      // originKey: 'fk_beneficiaryId',
      // originKeyTarget: 'id',
      // });

      collection.addOneToOneRelation('nv_subscription', 'NV_subscriptions', {
        originKey: 'fk_beneficiaryId',
        originKeyTarget: 'id',
      });

      // collection.addOneToManyRelation('nv_refundRequests', 'NV_refundRequests', {
      // originKey: 'fk_beneficiaryId',
      // originKeyTarget: 'id',
      // });

      // FIELDS
      collection.importField('user_email', { path: 'fk_user:email', readonly: true});
      collection.importField('user_phone', { path: 'fk_user:phone', readonly: true});
      collection.importField('user_firstname', { path: 'fk_user:firstname', readonly: true});
      collection.importField('user_status', { path: 'fk_user:status', readonly: true});
      collection.importField('user_lastname', { path: 'fk_user:lastname', readonly: true});
      collection.importField('user_gender', { path: 'fk_user:gender', readonly: true});
      collection.importField('user_discoveryProcess', { path: 'fk_user:discoveryProcess', readonly: true});
      collection.addField('np_subscription_amount', {
        columnType: 'Number',
        dependencies: ['np_subscription:rk_pspSubscriptionId'],
        getValues: (records, context) =>
          records.map(async r => {
            if (r.np_subscription !== null) {
              const invoices = await stripe.listInvoices({
                subscription: r.np_subscription.rk_pspSubscriptionId,
                status: 'paid'
              });
              if (invoices !== null) {
                return invoices[0].amount_paid / 100;
              }
              return 0;
            }
            return 0;
          }),
      });
      collection.importField('np_contract_subscriptionDate', { path: 'np_contract:subscriptionDate', readonly: true});
      collection.importField('np_contract_startDate', { path: 'np_contract:subscriptionDate', readonly: true});
      collection.importField('np_plan_title', { path: 'np_contract:fk_plan:title', readonly: true});
      collection.importField('np_subscription_status', { path: 'np_subscription:status', readonly: true});
      collection.addField('np_coupon_code', {
        columnType: 'String',
        dependencies: ['np_subscription:rk_pspSubscriptionId'],
        getValues: (records, context) =>
          records.map(async r => {
            if (r.np_subscription !== null) {
              const invoices = await stripe.listInvoices({
                subscription: r.np_subscription.rk_pspSubscriptionId,
                status: 'paid'
              });
              return invoices[0].discount?.coupon?.name;
            }
            return null;
          }),
      });
      collection.addField('nv_subscription_amount', {
        columnType: 'Number',
        dependencies: ['nv_subscription:rk_pspSubscriptionId'],
        getValues: (records, context) =>
          records.map(async r => {
            if (r.nv_subscription) {
              const invoices = await stripe.listInvoices({
                subscription: r.nv_subscription?.rk_pspSubscriptionId,
                status: 'paid'
              });
              if (invoices !== null) {
                return invoices[0].amount_paid / 100;
              }
              return 0;
            }
            return 0;
          }),
      });
      // collection.importField('nv_contract_subscriptionDate', { path: 'nv_contract:subscriptionDate', readonly: true});
      // collection.importField('nv_contract_startDate', { path: 'nv_contract:startDate', readonly: true});
      // collection.importField('nv_product_name', { path: 'nv_contract:fk_product:name', readonly: true});
      // collection.addField('nv_contract_beneficiaries', {
      // columnType: 'String',
      // dependencies: ['nv_contract:beneficiaries'],
      // getValues: (records, context) =>
      // records.map(r => {
      // if (r.nv_contract?.beneficiaries?.length > 0) {
      // return true
      // }
      // return false;
      // }),
      // });
      // collection.importField('nv_contract_status', { path: 'nv_contract:status', readonly: true});
      // collection.importField('nv_contract_adherentId', { path: 'nv_contract:rk_adherentId', readonly: true});
      // collection.importField('nv_contract_endDate', { path: 'nv_contract:endDate', readonly: true});
      // collection.addField('nv_subscription_type', {
      // columnType: 'String',
      // dependencies: ['nv_contract:filename'],
      // getValues: (records, context) =>
      // records.map(r => {
      // if (r.nv_contract?.filename?.slice(0, 3) === "PV3") {
      // return "PV3";
      // }
      // return '';
      // }),
      // });
      // collection.addField('nv_coupon_code', {
      // columnType: 'String',
      // dependencies: ['nv_subscription:rk_pspSubscriptionId'],
      // getValues: (records, context) =>
      // records.map(async r => {
      // if (r.nv_subscription !== null) {
      // const invoices = await stripe.listInvoices({
      // subscription: r.nv_subscription.rk_pspSubscriptionId,
      // status: 'paid'
      // });
      // return invoices[0].discount?.coupon?.name;
      // }
      // return null;
      // }),
      // });
      // collection.addField('nv_subscription_channel', {
      // columnType: 'String',
      // dependencies: ['nv_contract:filename'],
      // getValues: (records, context) =>
      // records.map(async r => {
      // if (r.nv_contract?.filename?.slice(0, 3) === "PV3") {
      // return "web";
      // }
      // return "app";
      // }),
      // });

      // ACTIONS
    });

```

if I comment on all relationships starting with _nv\__, everything is displayed correctly  
and you can see the details when you click on it

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/82462503058b14f15315908bc24c4ff3e2acbf3b.png)

But if I uncomment the relation that start with nv\_, I can no longer access certain details.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/4/4a46e7dd75e1add3e740bfc4140ccb40906ec240.png)

But the surprising thing is that when I click on those with data beginning with nv\_ , it works.  
For the example, I click on the one with **Nv subscription amount** at **40.1**

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/a/aa7b11479913bac9e59783c7fb03c8cc4750c76f.png)  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/5/55b74a43c5a4a327c8815b71eb043af1af64ae2f.png)

Whereas if I click on one that is **0** I get the error

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/a/a9bc2c1f8f4081cce662c09af97da3d1edcf2b5b.png)

In the logs server I have this  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/optimized/2X/7/7de23c06ac418447334c9903d76e8a9fa1a50567_2_690x24.png)  
and console.log I have this : 500

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/a/a160de58d5d84251c524a86313c27c17bd496f88.png)

I’ve seen in the other tables that everything is fine, it’s only this beneficiaries table where when I add these relationships, I have this little problem

## Expected behavior

The relations I add can all be displayed, even if the data is empty in the relation I’m adding.

## Context

- Project name: Nostrum Care v3
- Team name: Op
- Environment name: Local
- Agent (forest package) name & version:  
“@forestadmin/agent”: “^1.0.0”,  
“@forestadmin/datasource-sequelize”: “^1.5.21”,  
“@forestadmin/datasource-sql”: “^1.0.0”,  
“dotenv”: “^16.0.1”,  
“pg”: “^8.8.0”,  
“sequelize”: “^6.33.0”,  
“stripe”: “^14.17.0”
- Database type: Postgresql

Thank you in advance for your help !!

---

<div class="post-metadata">

**Author:** ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)\
**Post date:** [September 16, 2024, 10:25am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/2 "2024-09-16T10:25:30Z")

</div>

Hello @jacques_liao,

Can you please share the exact version of the agent that you are running ? You provided the minimun version “^1.0.0” from the `package.json`, but you should be able to find the resolved version in your `.lock` file (package.lock, yarn.lock or similar).

I believe the issue that you are experiencing has been resolved in a more recent agent version, so if you could try to upgrade to the latest version 🙏 and test in this context ?

Thanks

@Nicolas

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [September 16, 2024, 10:46am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/3 "2024-09-16T10:46:59Z")

</div>

Hello @anon7311026

I have this version :

```
"node_modules/@forestadmin/agent": {
  "version": "1.36.17",

```

If is not the correct version, what is the command to update the version of the forest admin agent version ?

Thanks

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [September 17, 2024, 6:49am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/4 "2024-09-17T06:49:26Z")

</div>

Hello @jacques_liao 👋

So you will need to upgrade your version to the latest one. And also all `@forestadmin/*` packages to benefit from all our fixes 💪

> [@jacques\_liao](#):
>
> If is not the correct version, what is the command to update the version of the forest admin agent version ?

That depends on your package manager (npm/yarn/pnpm), they provide commands to directly bump version.

But a simple way of doing it is to update your `package.json` manually and run a `yarn install` or `npm install`

You can found the latest version in there:

> **[@forestadmin/agent - npm search](https://www.npmjs.com/search?q=%40forestadmin%2Fagent)**

And if you want to resources on package manager comparisons.

> <https://stackoverflow.com/questions/16073603/how-can-i-update-each-dependency-in-package-json-to-the-latest-version>

> **[Difference between npm and yarn - GeeksforGeeks](https://www.geeksforgeeks.org/difference-between-npm-and-yarn/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [September 18, 2024, 8:43am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/5 "2024-09-18T08:43:15Z")

</div>

Hello @morganperre @anon7311026,

I was able to update **@forestadmin/agent** to 1.43.0 with command

```bash
npm i @forestadmin/agent

```

forestadmin-schema.json :

```json
  "meta": {
    "liana": "forest-nodejs-agent",
    "liana_version": "1.43.0",
    "liana_features": null,
    "stack": {"engine": "nodejs", "engine_version": "21.6.2"}
  }

```

package.json :

```json
  "dependencies": {
    "@forestadmin/agent": "^1.43.0",
    "@forestadmin/datasource-sequelize": "^1.5.21",
    "@forestadmin/datasource-sql": "^1.0.0",
    "dotenv": "^16.0.1",
    "moment": "^2.30.1",
    "pg": "^8.8.0",
    "sequelize": "^6.33.0",
    "stripe": "^14.17.0"
  },

```

But I have this error in console.log

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/7/7220be9a40d0d0cef0687f2293541511e16d1274.png)  
And this error in server  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/f/f8924f7bc095d6e54fd638214639c674e1969369.png)

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [September 18, 2024, 1:01pm UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/6 "2024-09-18T13:01:09Z")

</div>

Hello @jacques_liao,

Sorry, so let’s try to deep dive into your issue.

You should be able to view the content of the error in your console by opening it. And you can also check the **Network** tab of the browser Developer tools to see the call (if you open the response you should have the Error object with more detail explainations).

Another question:

- Are you using the [agent log level (`loggerLevel` agent option) **Debug**](https://docs.forestadmin.com/developer-guide-agents-nodejs/getting-started/install/create-your-agent#logger-function-and-loggerlevel-string-defaults-to-info) ?

Kind regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [September 18, 2024, 2:45pm UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/7 "2024-09-18T14:45:57Z")

</div>

This is what I get as an error in the logs

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/c/c060ab314f6aead64b20c10f80c96c80e2068f61.png)

And in network tab, I have this same error  
But I don’t see where this 500 error could come from.

And to answer your question, no, I don’t use the log agent level. But I’ll look into it !

Thanks

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [September 19, 2024, 1:02pm UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/8 "2024-09-19T13:02:52Z")

</div>

> [@jacques\_liao](#):
>
> And to answer your question, no, I don’t use the log agent level. But I’ll look into it !

Let us know if you get more agent logs with this setup.

Kind regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [September 20, 2024, 8:08am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/9 "2024-09-20T08:08:08Z")

</div>

Hello @morganperre

I have this logs in server

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/8648e06d36dfbed00755af2b27ff4d1bce1f8de9.png)

I don’t see any errors in the log 😕

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [September 23, 2024, 7:57am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/10 "2024-09-23T07:57:32Z")

</div>

Hello,

do you have any news about this problem 🙏?

Best regards

---

<div class="post-metadata">

**Author:** ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)\
**Post date:** [September 23, 2024, 12:13pm UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/11 "2024-09-23T12:13:45Z")

</div>

Hello @jacques_liao,  
This is difficult to provide more guidance from us, with no stack trace from the agent or more details from the frontend errors.  
Could you provide an [HAR extract](https://support.google.com/admanager/answer/10358597?hl=en) of the frontend rather than screenshots? this way we will be able to dig through the list of queries/replies, and maybe find details.

Did you try running the SQL query in the logs leading up to the errors, to see if one of them could be malformed or not working on your database ?

I’m surprised to see that a 404 error on agent side would result in a 500 error on frontend. Are you sure that those match ?

Thanks 🙏

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [September 23, 2024, 1:33pm UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/12 "2024-09-23T13:33:25Z")

</div>

Hello @anon7311026

Yes, I understand you completely. how can I send you the HAR extract data? as the content is too large for forest comunity

Yes, it works fine on our side with SQL commands

And yes, apart from this error I have nothing else.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)\
**Post date:** [September 23, 2024, 3:31pm UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/13 "2024-09-23T15:31:47Z")

</div>

How large is the HAR file ?  
Please record just enough around the error encountered so that it is limited in size.  
Please also note that it may contain sensitive info, so it is probably better to send it over through private message 🙏

---

<div class="post-metadata">

**Author:** ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)\
**Post date:** [September 24, 2024, 8:59am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/14 "2024-09-24T08:59:42Z")

</div>

> [@jacques\_liao](#):
>
> Local

Would you happen to work in an agent that is marked as `production` ? If so, then your stack traces will be removed, and you will need to ensure that you are working in a dev environment to ensure that your stack traces show up. 🙏

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [September 25, 2024, 7:46am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/15 "2024-09-25T07:46:27Z")

</div>

_Links removed for security_

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [September 25, 2024, 7:52am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/16 "2024-09-25T07:52:16Z")

</div>

Yes indeed, I work in production.  
I didn’t quite understand, i.e. aren’t the errors displayed in production?

---

<div class="post-metadata">

**Author:** ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)\
**Post date:** [September 25, 2024, 9:06am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/17 "2024-09-25T09:06:14Z")

</div>

Hello,  
No, they are not, for security purposes.  
The standard workflow is to work in a dev environment and solve the issues that you have there before commiting to production.

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [September 25, 2024, 10:03am UTC](https://community.forestadmin.com/t/new-forest-admin-agent-node-js-adding-relationships-with-other-relationships-that-cause-problems/7448/18 "2024-09-25T10:03:03Z")

</div>

Thanks to this, I was able to correct the bugs  
in fact, it was linked to an addField that was mismanaged on my side
