Announcement

Collapse
No announcement yet.

Array.indexOf() - Mirth error: Cannot find function indexOf.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Array.indexOf() - Mirth error: Cannot find function indexOf.

    Consider this snippet from a Code Template:

    Code:
        var someMnemonics = globalMap.get('someMnemonics');// holds the values ['FOO', 'BAR','BAT','BAZ','ROFL'];
    
        channelMap.put('someMnemonics length', someMnemonics.length);
        //this actually outputs "5.0" to the channel map.
    
        return (someMnemonics.indexOf('BAT') > -1);
    Here's the error we're seeing.
    ERROR MESSAGE: Error evaluating transformer
    com.webreach.mirth.server.MirthJavascriptTransform erException:

    DETAILS: TypeError: Cannot find function IndexOf.
    Does Mirth not support the indexOf method on arrays?!
    Last edited by Philoushka; 09-24-2009, 07:48 AM. Reason: Mule, Mirth, whatever... :)

  • #2
    I too have had trouble when directly referencing the global map. In most cases, I have taken to storing objects, which in turn store the data I wish to reference. I'm posting this code without specifically testing it, but it should give you an idea of what I do:

    Code:
    // to declare
    var MnemonicsArray = {mnemonics:['FOO', 'BAR','BAT','BAZ','ROFL']};
    globalMap.put('MnemonicsArray',MnemonicsArray);
    
    // to access
    var someMnemonics = globalMap.get('MnemonicsArray').mnemonics

    Comment


    • #3
      ...but in this case, it's not going to work either way. The indexOf method is simply not supported in all Javascript engines - and Mirth's Javascript engine is one of the non-supporting ones.

      Comment


      • #4
        indexOf does work, as we have used it to do a find and replace for field delimiters in a flat file. The only difference being we used the method against a String.

        Try storing the resulting into a variable and return the variable:

        MnemonicsArrayIndex = MnemonicsArray.indexOf('BAT');
        return(MnemonicsArrayIndex);

        See the associated post, which includes the use of the indexOf method.
        Mazhar Malik
        North Cumbria Informatics Service NHS

        Comment


        • #5
          Apolgies, forgot to include the link to the associated post.

          http://www.mirthcorp.com/community/f...ead.php?t=2989
          Mazhar Malik
          North Cumbria Informatics Service NHS

          Comment


          • #6
            I've used indexOf on strings as well. However, Philoushka was attempting to use it on an Array, and it turns out that not all Javascript engines have implemented indexOf on Arrays - though I'm not sure why.

            Unfortunately, I strongly suspect that assigning the result of an illegal operation to a variable will not cure the fact that the operation is illegal. A reasonable solution at this point would be to implement an implicit ad hoc indexOf simply by iterating through the elements, and returning the index of the first one that matches.

            Cheers all,

            ColinSJ

            Comment

            Working...
            X