Skip to main content
michaels14629018
New Participant
November 29, 2023
Question

CF2023: Elvis-operator don't work in extended component.

  • November 29, 2023
  • 2 replies
  • 357 views

Base.cfc:

 

component accessors="true" {
 public string function getFilterSql(required array filter) {
   ... filter["operator"] ?: ""  //null-exception
   ... filter.operator ?: ""  //null-exception
   ... isDefined('filter.operator') ? filter.operator : ""  //works
 }

 

Comp.cfc:

component extends="Base" rest="true" produces="application/json" restpath="xyz" {
 remote struct function getX() httpMethod="GET" {
  var filterSql = getFilterSql(filters);
 }
}

When I copy the function from Base.cfc into Comp.cfc ELVIS is working as it is suppose to.

Thank you in advance!

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    April 7, 2024
    quote

    When I copy the function from Base.cfc into Comp.cfc ELVIS is working as it is suppose to.


    By @michaels14629018

     

    You cannot yet be sure. The fact that you no longer get an error when you copy the function to Comp.cfc is not proof that things are working as they should. The code does in fact contain errors besides the null exception. Such errors just might be related to the null exception. In any case, it would help clarify the null problem if you first resolved all other issues.

    For example,

    1.  The functions getFilterSql and getX both have return-types. Yet neither contains a return statement.
    2.  The argument of getFilterSql, filter, is an array, whereas the function uses it as if it were a struct (as @EddieLotter has pointed out) .
    3.  The function getX uses an undefined variable, filters

     

    EddieLotter
    Inspiring
    November 29, 2023

    It's possible it is failing because your function signature defines "filter" as an array, but you are using filter as a struct in the function body.

    michaels14629018
    New Participant
    November 29, 2023

    Thanks for your reply EddieLotter!

    "filter" contain an array of structures. As mentioned: When I copy the function from Base.cfc into Comp.cfc it works fine (without changing anyhing).