# Objects that are no longer in datasource still tries to calculate a runtime function parameter (and fail)

**URL:** https://community.appfarm.io/t/objects-that-are-no-longer-in-datasource-still-tries-to-calculate-a-runtime-function-parameter-and-fail/1509
**Category:** Ask the community
**Created:** [November 14, 2024, 9:18am UTC](https://community.appfarm.io/t/objects-that-are-no-longer-in-datasource-still-tries-to-calculate-a-runtime-function-parameter-and-fail/1509 "2024-11-14T09:18:11Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Johan18](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/johan18/32/748_2.png) [@Johan18](https://community.appfarm.io/u/Johan18)
#### Post date: [November 14, 2024, 9:18am UTC](https://community.appfarm.io/t/objects-that-are-no-longer-in-datasource-still-tries-to-calculate-a-runtime-function-parameter-and-fail/1509/1 "2024-11-14T09:18:11Z")

</div>

Hi  
I have a datasource with a runtime function parameter that calculate a line price based on quantity and price, something like this:  
if (!quantity) return price\*quantity

My issue is that if this object is removed from the datasource by setting a flag (deleted=true is the datasource filter), the function still tries to execute the function parameter to get the line price, even though the object is no longer in the datasource. This generates errors which I want to get rid of.

_||message: Error running function: Cannot read properties of undefined (reading ‘quantity’),|_  
_|—|—|_  
_||functionName: fn49074aa3,|_  
_||functionParameters: [|_

This error is shown in dev console every time an object is removed.

Is it possible to avoid the errors without changing the way the datasource behaves? It does not help to check if quantity is set since the object itself is no longer in the datasource.

This looks like some kind of timing issue, the function parameter is executing on objects that no longer exist in the datasource - and fails because the object is no longer there.

Regards

---

<div class="post-metadata">

### Author: ![rhys](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/rhys/32/1379_2.png) [@rhys](https://community.appfarm.io/u/rhys)
#### Post date: [November 18, 2024, 8:40am UTC](https://community.appfarm.io/t/objects-that-are-no-longer-in-datasource-still-tries-to-calculate-a-runtime-function-parameter-and-fail/1509/2 "2024-11-18T08:40:18Z")

</div>

Hi Johan,

As a possible solution to removing these error messages from the console, you could look at implementing [nullish coalescing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing). This is a way to control the output or logic based on if a variable or property is null/undefined.

In this case, you could do something like:

```auto
let quantityToUse = quantity ?? 0
let priceToUse = price ?? 0
return quantityToUse * priceToUse

```

In this case, if either quantity or price are null, the sum will return 0.

Thanks  
Rhys

---

<div class="post-metadata">

### Author: ![Johan18](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/johan18/32/748_2.png) [@Johan18](https://community.appfarm.io/u/Johan18)
#### Post date: [November 18, 2024, 9:33am UTC](https://community.appfarm.io/t/objects-that-are-no-longer-in-datasource-still-tries-to-calculate-a-runtime-function-parameter-and-fail/1509/3 "2024-11-18T09:33:11Z")

</div>

Hi Rhys  
Thanks for your suggestion. I tried it out but the issue remains. The error is happening when trying to read the self property quantity on an object which is no longer in the datasource (which triggers a formula recalculation). quantity is one of my function params. So it does not really matter what I do inside the function.

If i just do “return 0” in the function and keep my function properties, the error is still displayed when an object is removed. It does not matter if I use the self property or the database connected property.

---

<div class="post-metadata">

### Author: ![kristian](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/kristian/32/58_2.png) [@kristian](https://community.appfarm.io/u/kristian)
#### Post date: [November 18, 2024, 10:13am UTC](https://community.appfarm.io/t/objects-that-are-no-longer-in-datasource-still-tries-to-calculate-a-runtime-function-parameter-and-fail/1509/4 "2024-11-18T10:13:17Z")

</div>

Hi,

This is not a general case, just tested various scenarios of the same, without any luck of recreation any error message in the log.

Can you try to isolate the case (e.g. recreate in a separate app), and provide more info on the data source setup, the function setup itself, and how the action is set up?

---

<div class="post-metadata">

### Author: ![Johan18](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/johan18/32/748_2.png) [@Johan18](https://community.appfarm.io/u/Johan18)
#### Post date: [November 18, 2024, 11:15am UTC](https://community.appfarm.io/t/objects-that-are-no-longer-in-datasource-still-tries-to-calculate-a-runtime-function-parameter-and-fail/1509/5 "2024-11-18T11:15:16Z")

</div>

Thanks.  
After trying to remove all function parameters it turns out the issue is not where I thought it was after all. The problem now is to find out where the issue is caused. I have a function ID but afaik I can´t pin point where this is based on the function id?

Error running function: Cannot read properties of undefined (reading ‘quantity’)

2  
{  
“message”: “Error running function: Cannot read properties of undefined (reading ‘quantity’)”,  
“functionName”: “fn49074aa3”,  
“functionParameters”: [  
{  
“id”: “otIjkM”,  
“name”: “orderline”,  
“functionParameterType”: “data\_source”,  
“dataSourceId”: “uYNfDK”,  
“selectionType”: “filtered”,  
“filterDescriptor”: {  
“id”: “4sffXU”,  
“nodeType”: “group”,  
“sourceDataSourceId”: “uYNfDK”, ( **Orderline** )  
“operator”: “$and”,  
“nodes”: [  
{  
“id”: “WUqKdG”,  
“nodeType”: “edge”,  
“operator”: “$eq”,  
“targetType”: “reference”,  
“targetDataSourceId”: “fLsJ6x”, ( **Product** )  
“sourceDataSourceId”: “uYNfDK”,  
“sourceNodeName”: “product”,  
“sourceDataType”: “reference”,  
“isSourceCardinalityMany”: false,  
“targetNodeName”: “\_id”,  
“isBaseTypeObjectId”: true,  
“targetDataSourceCardinality”: “MANY”,  
“targetSelectionMode”: “selected”  
}  
]  
},  
“parameterCardinality”: “MANY”,  
“nodeNamesInUse”: [  
“quantity”,  
“order”  
]  
}  
],  
“functionId”: “49074aa3”,  
“arguments”: {  
“AF\_LOGGER”: {  
“id”: “332de9e7”,  
“logName”: “default”,  
“root”: null,  
“parent”: null,  
“enabled”: true,  
“level”: 1,  
“metadata”: {  
“prefix”: “Appfarm”  
},  
“transports”: [  
{  
“id”: “console”,  
“level”: 1  
},  
{  
“id”: “devToolsLog”,  
“level”: 1  
}  
],  
“children”: ,  
“timers”: {}  
},  
“orderline”:   
},  
“error”: {}  
}

The property “quantity” is used a lot of places throughout the app, and in many views (not active though) and actions, but it does not seem to happen in an action.

I know the two data sources referenced in the error are Orderline and Product…  
From the error it looks like its a function parameter that is recalculated and the only place Im aware of is … the place I was working on.

---

<div class="post-metadata">

### Author: ![ErikAKSkallevold](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/erikakskallevold/32/1378_2.png) [@ErikAKSkallevold](https://community.appfarm.io/u/ErikAKSkallevold)
#### Post date: [November 20, 2024, 12:43pm UTC](https://community.appfarm.io/t/objects-that-are-no-longer-in-datasource-still-tries-to-calculate-a-runtime-function-parameter-and-fail/1509/6 "2024-11-20T12:43:30Z")

</div>

> [@Johan18](#):
>
> if (!quantity) return price\*quantity

Just to chime in,  
Is the code example that you provided correct?

You seem to check if quantity has value, and **if it doesnt** you attempt to use it. Is that correct?

---

<div class="post-metadata">

### Author: ![Johan18](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/johan18/32/748_2.png) [@Johan18](https://community.appfarm.io/u/Johan18)
#### Post date: [November 20, 2024, 1:24pm UTC](https://community.appfarm.io/t/objects-that-are-no-longer-in-datasource-still-tries-to-calculate-a-runtime-function-parameter-and-fail/1509/7 "2024-11-20T13:24:37Z")

</div>

Hi  
Thanks for following up.  
I havent had the time to track this down yet but I verified that it is not a general issue as Kristian pointed out so it must be some kind of dependency issue when removing an object.

No, the code was not correct, the check was: if (!quantity) return 0
