User defined function doesn´t work because I use split method

Prachar Marek 20 Reputation points
2023-10-11T15:48:50.1033333+00:00

I created user defined function in Azure stream analytics but this function doesn´t work because I use split method in my function

Erorr code:
Function 'splitandconverthex' resulted in an error: 'TypeError: Unable to get property 'split' of undefined or null reference' Stack: TypeError: Unable to get property 'split' of undefined or null reference at splitAndConvertHex (Unknown script code:3:3)

This is my function:

function splitAndConvertHex(hexString) {
  // "03.00.20.80.0F.00.00.68.98.01" --> 198
  let x = parseInt(hexString.split('.').slice(-2).reverse().join(''));
  const firstNumber = parseInt((x / 10).toString(), 16);
  const secondNumber = parseInt((x % 10).toString(), 16);
  
  // Převod na float hodnotu
  const floatValue = parseFloat(`${firstNumber}.${secondNumber}`);
  return floatValue;
}

And this is my SQL querry:

WITH DallasData AS (
    SELECT 
    *
    FROM
        DallasInput
    WHERE
    data.rsp.rData IS NOT NULL
)
SELECT 
data.rsp.rData as TemperatureHex,
EventProcessedUtcTime as Time
UDF.splitAndConvertHex(TemperatureHex) as Temp

INTO [DallasOutput]
FROM [DallasData]

Can you help me with this problem

Azure Stream Analytics
Azure Stream Analytics
An Azure real-time analytics service designed for mission-critical workloads.
331 questions
0 comments No comments
{count} votes

Accepted answer
  1. QuantumCache 20,031 Reputation points
    2023-10-11T21:17:20.23+00:00

    Hello @Prachar Marek

    Did you try adding a null check for the hexString parameter before calling the split method. Here is an updated version of your TypeScript code that includes the null check:

    function splitAndConvertHex(hexString) {
      if (hexString == null) {
        return null;
      }
      // "03.00.20.80.0F.00.00.68.98.01" --> 198
      let x = parseInt(hexString.split('.').slice(-2).reverse().join(''));
      const firstNumber = parseInt((x / 10).toString(), 16);
      const secondNumber = parseInt((x % 10).toString(), 16);
      
      // Převod na float hodnotu
      const floatValue = parseFloat(`${firstNumber}.${secondNumber}`);
      return floatValue;
    }
    
    
    

    With this null check in place, the function will return null if the hexString parameter is null or undefined, and the split method will not be called.

    Another thing to consider is the format of the input data. Make sure that the TemperatureHex field in your input data is in the expected format (i.e. "03.00.20.80.0F.00.00.68.98.01"). If the input data is not in the expected format, this could cause issues with the function.

    I hope this helps! If you have any further questions or concerns, please let me know in the below comment section.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful