# Custom Field Values of Nested Model Not Loading

**URL:** <https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645>\
**Category:** Help me!\
**Created:** [September 8, 2023, 12:52pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645 "2023-09-08T12:52:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 8, 2023, 12:52pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/1 "2023-09-08T12:52:38Z")

</div>

## Feature(s) impacted

I included a repo to illustrate my problem:

> **[GitHub - droegier/forestadmin-custom-field-bug](https://github.com/droegier/forestadmin-custom-field-bug)**
>
> Contribute to droegier/forestadmin-custom-field-bug development by creating an account on GitHub.

The custom field of a nested model is not loading, when placed as a reference for that nested model.

## Observed behavior

With my repo:

1. in the layout, set fullAddress as the reference field for “clients\_address”.
2. Create a new client
3. For that client fill in the address
4. Go to that client’s detail view.

You will should see the address relationship is falsely displayed with “null, null null”

## Expected behavior

Instead I would expect the relationship to appear with “Street Nr, Postal Code Municipality”

## Failure Logs

None

## Context

Can be extracted from the repo.

---

<div class="post-metadata">

**Author:** ![anon34731316](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@anon34731316](https://community.forestadmin.com/u/anon34731316)\
**Post date:** [September 8, 2023, 2:32pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/2 "2023-09-08T14:32:29Z")

</div>

Hello @David_Roegiers,

Thanks for reaching out and for the repo 🙌

Sorry to hear you’re facing this inconvenience.  
I’m going to try to reproduce on my side and I’ll get back to you.

Cheers!

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 11, 2023, 10:02am UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/3 "2023-09-11T10:02:14Z")

</div>

@anon34731316

You will see, in my repo, that I have configured the flattener for the mongoose model as follows:

clients model

```javascript
...
address: {
 street: string,
 municipality: string
},
...

```

clients flattenOptions

```javascript
{
    asModels: ['address']
}
...

```

Are non-array nested object fields in the data model, allowed to be flattened as models?

In that case, sorry for the false bug report. I did not get it from your documentation, that this is a no-go. Anyway, that is what I did and might the reason why we are seeing this behaviour. Moreover, it causes another bug on creation of a new `address`. If this is indeed not supported behaviour, maybe might be cool to implement a fail-safe informing the developer through an error, that this sort of configuration is not possible, i.e. only array fields may be configured `asModels`.

FYI @anon39848301

---

<div class="post-metadata">

**Author:** ![GuillaumeGautreau](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaumegautreau/32/557_2.png) [@GuillaumeGautreau](https://community.forestadmin.com/u/GuillaumeGautreau)\
**Post date:** [September 11, 2023, 1:28pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/5 "2023-09-11T13:28:57Z")

</div>

Hello @David_Roegiers,

I’m taking over this subject. Thanks a lot for the minimal example reproducing your issue. I’ll investigate on my side and get back to you.

---

<div class="post-metadata">

**Author:** ![GuillaumeGautreau](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaumegautreau/32/557_2.png) [@GuillaumeGautreau](https://community.forestadmin.com/u/GuillaumeGautreau)\
**Post date:** [September 12, 2023, 8:49am UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/6 "2023-09-12T08:49:47Z")

</div>

For information, I’m working on a fix on your first problem regarding the computed field “full address” not being correctly retrieved when used as a reference field.

Using `asModels` on a non-array field should be supported because it’s not a lot different from using it on array fields. Right now it seems to have some limitations, but we’ll fix them.

I’ll work on a second fix regarding the creation when the first one will be released.

Please note though that address creation will only be possible on an existing client: as addresses are attached to client objects, it won’t be possible to create an address first, and then a client.

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 12, 2023, 9:45am UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/7 "2023-09-12T09:45:36Z")

</div>

Okay, thanks @GuillaumeGautreau.

That’s good to hear. It would be helpful to use asModels on a non-array field. I will apply this to visually improve the structure of the Details view of field-heavy collections.

I’ll wait for your fixes then.

> [@GuillaumeGautreau](#):
>
> Please note though that address creation will only be possible on an existing client: as addresses are attached to client objects, it won’t be possible to create an address first, and then a client.

That makes sense. Is there a way to programmatically avoid this option to appear in the UI? Our operators will find it and then report this as a bug.

---

<div class="post-metadata">

**Author:** ![GuillaumeGautreau](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaumegautreau/32/557_2.png) [@GuillaumeGautreau](https://community.forestadmin.com/u/GuillaumeGautreau)\
**Post date:** [September 12, 2023, 11:39am UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/8 "2023-09-12T11:39:48Z")

</div>

Hello @David_Roegiers,

We released `@forestadmin/datasource-mongoose@1.5.15` fixing the issue with computed fields. You can upgrade to this version and use computed fields as expected.

Regarding the possibility to hide the address creation, I would suggest to not give the right to create addresses to users. You can remove this possibility in the roles tab in your project settings (once deployed in production or on a remote environment).

It might be useful though to create addresses along with clients, which is possible with the option … `asFields` but at the moment you cannot use both `asFields` and `asModels` at the same time on the same field.

I think it might be possible using the `flattener` plugin (see the [docs here](https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/plugins/provided-plugins/flattener)) in addition to `asModel`. But right now we have the same creation bug than the one we need to fix.

I’ll let you know once done.

---

<div class="post-metadata">

**Author:** ![GuillaumeGautreau](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaumegautreau/32/557_2.png) [@GuillaumeGautreau](https://community.forestadmin.com/u/GuillaumeGautreau)\
**Post date:** [September 14, 2023, 9:14am UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/9 "2023-09-14T09:14:08Z")

</div>

We released @forestadmin/datasource-mongoose@1.5.17 that fixed the issue with record creation and retrieval.

Could you please give it a try?

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 14, 2023, 10:04am UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/10 "2023-09-14T10:04:19Z")

</div>

Thanks @GuillaumeGautreau

I’m just waiting for this fix to be merged, and then I will try them both out in the afternoon, if it’s ready by then.

> [@Failing: reading 'foreignCollection'](https://community.forestadmin.com/t/failing-reading-foreigncollection/6591/29):
>
> Ok, but unfortunately this downloads the source files and not the distribution, so the build breaks on my repository. I saw, that the PR is reviewed, so I’ll wait for it to be merged and released.

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 20, 2023, 12:10pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/11 "2023-09-20T12:10:00Z")

</div>

@GuillaumeGautreau

Sorry for the late reply, I was focused on some other threads.

So, I’m running `@forestadmin/datasource-mongoose:1.5.19`.

There is a non-array nested object field of my MongoDB data model, called `x`. I added `x` to the `asModels` array of the built-in field flattener for that model.

Unfortunately, when I navigate to the computed record, which corresponds to that field `x` (detail view), the server freezes, i.e. the HTTP request stalls.

Any idea why this might be the case?

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 20, 2023, 1:23pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/12 "2023-09-20T13:23:02Z")

</div>

Let me add some more information, on top of the analogy I described earlier (with the flattened model field `x`):

**This FA server request works:**  
[http://localhost:3310/forest/parent\_x?timezone=Europe%2FZurich&fields[lawyers\_billing]=\_id%2CbexioContact%2CbexioContactId%2Cparent%2CstripeCustomerId&fields[parent]=fullName&page[number]=1&page[size]=15&sort=-\_id](http://localhost:3310/forest/parent_x?timezone=Europe%2FZurich&fields%5Blawyers_billing%5D=_id%2CbexioContact%2CbexioContactId%2Cparent%2CstripeCustomerId&fields%5Bparent%5D=fullName&page%5Bnumber%5D=1&page%5Bsize%5D=15&sort=-_id)

**While, this FA server request stalls:**  
[http://localhost:3310/forest/lawyers\_x/64a1eca47c0ce8420edc95fc.x?timezone=Europe%2FZurich](http://localhost:3310/forest/lawyers_x/64a1eca47c0ce8420edc95fc.x?timezone=Europe%2FZurich)

The logs don’t say anything, and I haven’t customized the `lawyers_x` collection.

🤔

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 20, 2023, 1:26pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/13 "2023-09-20T13:26:19Z")

</div>

It’s not much information, but maybe you can suggest me a line number in the distribution, where to put a breakpoint (js, not ts) and share some variable data?

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 20, 2023, 1:40pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/14 "2023-09-20T13:40:27Z")

</div>

It seems, that the issue might be coming from something else. I’m investigating and will get back to this thread.

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 20, 2023, 1:56pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/15 "2023-09-20T13:56:44Z")

</div>

So, the faulty behaviour is happening, if I upgrade @forestadmin/datasource-mongoose from 1.5.15 to 1.5.16. I once saw a `MongoPoolClearedError`, but I am not certain if this is the culprit.

In this situation, I can not upgrade past 1.5.15 to test the new functionality, so we would need to fix this, before I can mark this issue as solved.

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 21, 2023, 7:03am UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/16 "2023-09-21T07:03:49Z")

</div>

> [@GuillaumeGautreau](#):
>
> We released `@forestadmin/datasource-mongoose@1.5.15` fixing the issue with computed fields. You can upgrade to this version and use computed fields as expected.

So, I can confirm, that this is working with version `1.5.15` and will mark this issued as Solved.

Nonetheless, I can’t upgrade beyond that, which is OK for me now, since I don’t have the issue about the field creation any more.

Still would be great if this freezing behaviour gets tackled at some point.

---

<div class="post-metadata">

**Author:** ![GuillaumeGautreau](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaumegautreau/32/557_2.png) [@GuillaumeGautreau](https://community.forestadmin.com/u/GuillaumeGautreau)\
**Post date:** [September 21, 2023, 7:57am UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/17 "2023-09-21T07:57:38Z")

</div>

Hello @David_Roegiers,

Is it possible to share a repository that reproduces this bug, as you did with the previous one? On my side when I navigated to records everything worked smoothly so I would need more info to reproduce the issue.

Thanks

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 25, 2023, 7:25am UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/18 "2023-09-25T07:25:00Z")

</div>

Hello @GuillaumeGautreau

In order to create a minimal reproducing repository, I want to quickly test the code in a ForestAdmin UI.

Setting this up as a ForestAdmin configuration, quickly, has been quite cumbersome so far for the following reasons:

- I don’t want this test to mess with the layout of my existing project.
- If I create a new ForestAdmin project, it seems very difficult to get past the onboarding process in the UI, which is the main approach I know.

What would you recommend, is the fastest way to get a FA server running with env vars, so I can test it out in the UI.

Thanks!

---

<div class="post-metadata">

**Author:** ![GuillaumeGautreau](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaumegautreau/32/557_2.png) [@GuillaumeGautreau](https://community.forestadmin.com/u/GuillaumeGautreau)\
**Post date:** [September 25, 2023, 1:16pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/19 "2023-09-25T13:16:28Z")

</div>

Using the following command, you’ll create a project and the file structure for your db:

```javascript
forest projects:create reproduce-bug -H localhost -P 3333 -c mongodb://localhost:27017/bug

```

With:

- `reproduce-bug` being your new project name (chose one that is not already taken)
- `-P` is the port number of your new agent
- `-H` is the host name of this new agent
- `-c` contains the connection string to your DB that will help reproduce the issue. This DB is introspected to create the corresponding Mongoose schemas.

You need to be logged in first, if it’s not the case, run `forest login` to authenticate your command line executable locally.

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 27, 2023, 1:25pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/20 "2023-09-27T13:25:41Z")

</div>

> [@GuillaumeGautreau](#):
>
> ```javascript
> forest projects:create reproduce-bug -H localhost -P 3333 -c mongodb://localhost:27017/bug
> 
> ```

Thanks. Mongodb is running locally on 27017, but I get the following error when setting up the project:

```javascript
× Cannot connect to the database due to the following error:
× {"name":"MongoServerSelectionError","reason":{"type":"Single","setName":null,"maxSetVersion":null,"maxElectionId":null,"servers":{},"stale":false,"compatible":true,"compatibilityError":null,"logicalSessionTimeoutMinutes":null,"heartbeatFrequencyMS":10000,"localThresholdMS":15,"commonWireVersion":null}}

```

---

<div class="post-metadata">

**Author:** ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)\
**Post date:** [September 27, 2023, 1:30pm UTC](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645/21 "2023-09-27T13:30:12Z")

</div>

`forest-cli/4.2.0 darwin-arm64 node-v18.16.1`

and

```javascript
mongo localhost:27017/bug

```

works properly

[Next page](https://community.forestadmin.com/t/custom-field-values-of-nested-model-not-loading/6645.md?page=2)
