Search code examples
google-bigquerybigquery-udf

Javascript chartAt is not a function at UDF$1(STRING)


I have the following function to convert any string to title case:

CREATE OR REPLACE FUNCTION udf.title_case(str STRING)
  RETURNS STRING
  LANGUAGE js AS """
  return str
          .replace(/([^\\W_]+[^\\s-]*) */g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();})
  """;

UPDATE:

I fixed chartAt to charAt and still get the same error

and it produces the following error:

"project.dataset.charAt" is not a function at UDF$1(STRING) line 3, columns 110-111

I can bypass this error by using [] notation which is not ideal however I hit the same error with substr.

I normally test my functions in JSBin or similar and works fine but when translate it to Bigquery I need to escape \ in regex and then deal with these out of the blue errors.

Makes life harder for those who are not experienced in the arts of JS programming.

Thanks in advance for your help.


Solution

  • You write the wrong function name, "char[t]At" instead of "charAt". I use temp function to test

    CREATE TEMP FUNCTION tempFunc(str STRING)
      RETURNS STRING
      LANGUAGE js AS """
      return str
              .replace(/([^\\W_]+[^\\s-]*) */g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();})
      """;
    
    
      select tempFunc("abce")
    

    and the result is "Abce". You can try this query in your bigquery editor