Error in a MongoDB insertOne query when loading app

Hello there,

I'm attempting to resolve this error in a MongoDB insertOne query while loading an application.

Error: Cannot read properties of null (reading '_id')

insertOneDerivedAttribute!

from insertOneDerivedAttribute.query update(insertOneDerivedAttribute)
from derivedAttributes.selectedRow update(derivedAttributes)
from user interaction

The query is really simple:
{
"derivedAttributeName": {{ formDerivedAttribute.data.textInputInternalName }},
"derivedAttributeType": {{ formDerivedAttribute.data.selectDerivedAttributeType }}
}

Could somebody help me?

Any thoughts are welcome.

Thanks,

Hey @uelintons, welcome to the forums! Would you be able to provide more context / send a screenshot of your query?

Hey @jamesg31,

Thanks for replying.

Sure, here the context:

I have a modal with a form to add new documents to the derivedAttributes collection in MongoDB.

Here the query screenshot:

These are the form's Submit button event handlers



The query works as expected from submitting new documents/records.

The error occurs only on the app's load.

Thanks in advance,

Do you want the query to run on first load? I am imagining this issue is occurring due to the data not yet being loaded into the form when the query is running on load.

No, I don't want to run on first load.
Since the query's Run behavior is set to Manual, I really don't know why it is trying to run on the first load.
I tried to put a condition in the Advanced -> Disable query without success.

Anybody have a clue or idea?

Thanks

Hi, @uelintons. Happy to help!
Does the modal have that title by default? Although the modal is closed, this may be the reason why the query is running on app load.

Hi @Paulo.

Thanks for helping us.
This is the modal title:
image

I understand that the title is "Add Attribute Type" on app load, so the query is starting due to the handler conditional.

Do you have any suggestions for the conditional?

Thanks,

Would triggering the query through an event handler that happens 'on Show' for the Modal work for your use case?

I'm afraid not.
The trigger needs to be related to the modal's submit button to insert a new document into the collection, only if the form has the "Add..." title.

I'm sorry, I thought we were doing something different. Thanks for clarifying!

The 'Only run when' is perfectly fine. If this query is working on submission of the form, and is set to run manually:

I would check the success event handler of the queries that run on page load, something might be triggering it.

If I turn to manual the query attached to the table element, the warning disappears. So I think this query is triggering somehow the insert query. But then I need to put an event handler to run the table query, which is not a good choice.
Is there a way to check if the page is loading? If so, I can add this condition to the "Only run when" condition of the event handler.

Could you share the event handler we have for 'Select row' on the table component?

From taking a closer look at the error:

It looks like a row is being selected by default, which may be triggering the query.

For example, with these table settings:

The notification shows on page load because the row gets selected automatically:

Edit: Try setting 'Default row' to 'None'

It was almost it.
You've given me the way.
With this setting ("Default row" to None), it worked:

Thank you very much!

Oops, that's what I meant, otherwise we wouldn't be able to select rows.
I'm glad we were able to figure it out.
Happy Building! :hammer_and_pick: