Calculation fields in Castor EDC/CDMS use JavaScript to compute values dynamically. When a calculation contains syntax mistakes, unquoted text, or missing variables, an error will appear in data entry or in the Calculation Helper: https://calculation-helper.tools.castoredc.com/
Use this guide to diagnose and resolve the most common calculation error messages.
1. "Not all values for this calculation are available (yet)"
This is not a syntax error. It indicates that one or more fields referenced in the calculation template currently have no data entered. By default, Castor waits until every referenced field has a value before running the calculation.
- Expected behavior: If the calculation should only run once all data is entered, no action is needed. The calculation will compute automatically as soon as the user enters the remaining values.
-
Force calculation with empty fields: If you want the calculation to run even when some fields are blank (for example, to check which fields are missing or calculate a partial score), add the
##allowempty##tag at the very top:
'##allowempty##'; if ("{field_a}" == "'NA'") { "Field A is required"; } else { "{field_a}"; }
2. Syntax Errors (Parentheses, Brackets, and Quotes)
A. Unexpected end of input
-
Why it happens: You opened a bracket
{, parenthesis(, or quotation mark, but did not close it before the end of the script. -
Incorrect:
'##allowempty##'; if ("{allergy}" == "'NA'") { 1; // Missing closing bracket } -
Correct:
'##allowempty##'; if ("{allergy}" == "'NA'") { 1; }
B. Unexpected identifier
-
Why it happens: This almost always occurs when referencing a variable using single quotes (
'{variable}') while using'##allowempty##'.- In Castor, an empty field under
##allowempty##is substituted with'NA'(wrapped in single quotes). - If your template uses single quotes:
'{variable}'$\rightarrow$ evaluates to''NA''. JavaScript sees an empty string followed by an unquoted word (NA), throwingUnexpected identifier.
- In Castor, an empty field under
-
Incorrect:
'##allowempty##'; if ('{allergy}' == "'NA'") { "No allergies reported."; } -
Correct (Always wrap string variables in double quotes):
'##allowempty##'; if ("{allergy}" == "'NA'") { "No allergies reported."; }
C. Invalid or unexpected token
-
Why it happens: A quotation mark was left unclosed, or invalid characters (such as curly/smart quotes copied from Microsoft Word:
“ ”) were pasted into the calculation editor. -
Incorrect:
'##allowempty##'; if ("{medication}" == "'NA'") { "No medication; // Missing closing quote } -
Correct:
'##allowempty##'; if ("{medication}" == "'NA'") { "No medication"; }
D. Unexpected token ')' or Unexpected token '{'
-
Why it happens: Unmatched or missing parentheses in an
ifcondition or function call. -
Incorrect:
'##allowempty##'; if ("{medication}" == "'NA'" { // Missing closing parenthesis ) "No medication"; } -
Correct:
'##allowempty##'; if ("{medication}" == "'NA'") { "No medication"; }
3. Date & Time Function Errors
Cannot read property '4' of null / Unable to get property of undefined or null reference
TypeError: Cannot read property '4' of null (or: Cannot read properties of null (reading '4'))
Built-in date functions (such as getDateDiff) expect valid date strings (e.g. DD-MM-YYYY). If you use '##allowempty##' and one of the date fields is empty, Castor passes 'NA' into getDateDiff. The date parser fails to split the string and crashes with this error.
Always wrap your date difference calculation in an if statement to verify that both dates are filled in before calling the function:
'##allowempty##';
// Only calculate difference if both dates have been entered:
if ("{date_start}" != "'NA'" && "{date_end}" != "'NA'") {
getDateDiff("{date_start}", "{date_end}", "d"); // "d" for days
} else {
""; // Return blank until both dates are available
}
Missing ) after argument list (with getTimeDiff)
-
Why it happens: Calling
getTimeDiffwhen one of the time variables is empty. -
Solution: If calculating with potentially empty time fields, add both empty-handling tags at the top of your calculation:
'##allowempty##'; '##setemptytozero##'; getTimeDiff("{time_start}", "{time_end}", "m");
4. Arithmetic & Output Errors
Output displays NaN
-
Why it happens:
NaNstands for "Not a Number". This occurs when:- A mathematical operation (
+,-,*,/) is performed on a variable containing text, missing data reason codes, or an empty'NA'string. - The record has a User Missing Value assigned (e.g., "Not applicable", "Unknown").
- A mathematical operation (
-
How to fix:
-
For numbers that might be empty, use
'##setemptytozero##'to treat unentered values as0:'##allowempty##'; '##setemptytozero##'; {score_1} + {score_2} + {score_3};
-
Output displays Infinity
-
Why it happens: Division by zero in JavaScript returns
Infinity. This is common in BMI calculations when height is0or empty: -
How to fix: Check that the denominator is greater than zero before dividing:
'##allowempty##'; var height_m = {height} / 100; if ({height} > 0 && {weight} > 0) { ({weight} / (height_m * height_m)).toFixed(1); } else { ""; }