Fix: NaN Issues in Expression Parser for One-to-Many Relationships - #86
Open
trinly01 wants to merge 2 commits into
Open
Fix: NaN Issues in Expression Parser for One-to-Many Relationships#86trinly01 wants to merge 2 commits into
trinly01 wants to merge 2 commits into
Conversation
…ead of 5
User's template {{ ROUND(ASUM(procurement_schedule, quantity)) }} was showing NaN instead of correct values
Root cause: Directus stores quantities as strings, causing string concatenation instead of arithmetic addition
Specific case: 2 schedules with quantities 2 and 3 showed 0 instead of 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix NaN Issues in Expression Parser for One-to-Many Relationships
Problem Description
When using the computed interface with Directus one-to-many (o2M) relationships, users were experiencing NaN (Not a Number) values in calculated fields. This was particularly problematic for:
Root Cause
"5" + "3"resulted in"53"instead of8Example Scenarios Where NaN Occurred
Solution Implemented
1. Comprehensive NaN Prevention Pattern
Applied
(+value || 0)pattern to ALL numeric operations:2. Operations Fixed
Aggregated Operations (Primary Fix):
ASUM- Aggregated sum of related itemsAMIN- Aggregated minimum of related itemsAMAX- Aggregated maximum of related itemsAAVG- Aggregated average of related itemsAMUL- Aggregated multiplication of related itemsUnary Operations:
ABS,SQRT,SUM,AVERAGE,CEIL,FLOOR,ROUND,EXP,LOG,MAX,MINBinary Operations:
SUM,SUBTRACT,MULTIPLY,DIVIDE,REMAINDER,ROUND,MAX,MIN,POWER3. Type Safety Improvements
4. Comprehensive Test Coverage
Added 65+ test cases covering:
Impact
✅ o2M relationships now work correctly with computed fields
✅ No more NaN values in aggregated calculations
✅ Proper string-to-number conversion for all Directus data
✅ Backward compatibility maintained with existing expressions
✅ Enhanced type safety throughout the expression parser
Example Usage After Fix
This fix ensures that computed interface expressions work reliably with all Directus relationship types, especially one-to-many relationships where aggregated calculations are essential.