Lee_Str
Lee_Str

Reputation: 3496

Snowflake UDF & Javascript - Issue with JavaScript compilation error: Uncaught SyntaxError

Disclaimer: I'm new to JavaScript & new to writing JavaScript UDFs in Snowflake.

I'm attempting to build a UDF which will "clean" the string of any characters where the ascii code is not between 32 & 127. When I attempt to use this function, I get the error:

Reason:
SQL Error [100131] [P0000]: JavaScript compilation error: Uncaught SyntaxError: missing ) after
argument list in CLEAN at '    FOR (i = 0; i < value.length; i++) {' position 13

After searching around for what in the syntax could be causing this problem, I'm no further along. The value being input is a length 3 string, with the following ascii codes: 0, 13, 0

CREATE OR REPLACE FUNCTION clean(value STRING)
    RETURNS string LANGUAGE JAVASCRIPT
    AS 
    $$ 
    var i = 0;
    var letter = "";
    var newValue = "";
    FOR (i = 0; i < value.length; i++) {
        letter = value[i].charCodeAt(0)
        IF ( letter >= 32 && letter <= 126) {
            newValue += value[i];
        } elseif (letter = 0) {
            newValue += value[i];
        }
    }
RETURN newValue
$$;

Upvotes: 1

Views: 2253

Answers (1)

Simon D
Simon D

Reputation: 6229

Two things:

1) JavaScript is a case sensitive so keywords like FOR, IF and WHILE need to be lowercased. (Thanks to @waldente for that answer)

Also:

2) Change all references to the input parameter from lowercase value to uppercase VALUE. For example change value.length in your for loop to VALUE.length

In the documentation on JavaScript UDFs there is this part that says:

Note that the JavaScript code must refer to the input parameter names as all upper-case, even if the names are not uppercase in the SQL code

Upvotes: 1

Related Questions