Hitachi Vantara Pentaho Community Forums
Results 1 to 9 of 9

Thread: Javascript Eval Function

  1. #1

    Default Javascript Eval Function

    Hi,

    When i am trying to access a variable of kettle as given below it works with eval.

    var week_1 = 35;
    eval("var x = Week_1");
    Alert(x);
    But when i try to do the same thing in this manner, it doesnt .

    var index = 1
    var week_1 = 35;
    eval("var x = Week_"+index);
    Alert(x);

    Following is the exception trace :

    2008/11/13 18:27:21 - Data Conversion Java Script 2.0 - ERROR (version 3.0.2, build 538 from 2008/02/06 13:13:19) : Unexpected error :
    2008/11/13 18:27:21 - Data Conversion Java Script 2.0 - ERROR (version 3.0.2, build 538 from 2008/02/06 13:13:19) : org.pentaho.di.core.exception.KettleValueException:
    2008/11/13 18:27:21 - Data Conversion Java Script 2.0 - ERROR (version 3.0.2, build 538 from 2008/02/06 13:13:19) : Javascript error:
    2008/11/13 18:27:21 - Data Conversion Java Script 2.0 - ERROR (version 3.0.2, build 538 from 2008/02/06 13:13:19) : ReferenceError: "Week_1" is not defined. (script#12(eval)#1)
    2008/11/13 18:27:21 - Data Conversion Java Script 2.0 - ERROR (version 3.0.2, build 538 from 2008/02/06 13:13:19) :
    2008/11/13 18:27:21 - Data Conversion Java Script 2.0 - ERROR (version 3.0.2, build 538 from 2008/02/06 13:13:19) : at org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesMod.addValues(ScriptValuesMod.java:443)
    2008/11/13 18:27:21 - Data Conversion Java Script 2.0 - ERROR (version 3.0.2, build 538 from 2008/02/06 13:13:19) : at org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesMod.processRow(ScriptValuesMod.java:738)
    2008/11/13 18:27:21 - Data Conversion Java Script 2.0 - ERROR (version 3.0.2, build 538 from 2008/02/06 13:13:19) : at org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesMod.run(ScriptValuesMod.java:798)
    2008/11/13 18:27:21 - Data Conversion Java Script 2.0 - ERROR (version 3.0.2, build 538 from 2008/02/06 13:13:19) : Caused by: org.mozilla.javascript.EcmaError: ReferenceError: "Week_1" is not defined. (script#12(eval)#1)

    Any pointers will help.

    Thanks.

  2. #2
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    JavaScript is case sensitive.

  3. #3
    Join Date
    May 2006
    Posts
    4,882

    Default

    .... both fail... but check the spelling of your variables (hint: it's case sensitive).

    When fixed both work.

    Regards,
    Sven

  4. #4

    Default

    Yes i agree.

    The only difference between two eval statements is that in one of them i have hardcoded the index as 1

    eval("var x = Week_1");

    in the second i have formed the string Week_1 appending the index.

    var index = 1;
    eval("var x = Week_"+index);

  5. #5
    Join Date
    May 2006
    Posts
    4,882

    Default

    aarggh ... if you use the case correctly, both your examples above work. In their current form above, BOTH fail.

    Not "Week_1", but "week_1" e.g.

    Regards,
    Sven

  6. #6

    Default

    ok.. i got it.. its just a typo...

    i have declared the variable as

    var Week_1 = 35

  7. #7
    Join Date
    May 2006
    Posts
    4,882

    Default

    Now also fix the typo in the examples, and both work.

    Regards,
    Sven

  8. #8

    Default

    Sven,

    That worked.
    But now i am trying it this way.

    I have set of fields from a text file input step

    Week_1,Week_2 ... Week_10

    So now when i am trying to do the same thing using eval, it displays

    org.mozilla.javascript.EcmaError: ReferenceError: "Week_1" is not defined. (script#12(eval)#1)

    Does it makes difference, if its a variable defined within javascript and if it is and input to javascript step from TextFileInput

  9. #9
    Join Date
    May 2006
    Posts
    4,882

    Default

    Apparently it does make a difference between local defined and fields... but there are easier ways to get at the values of fields in a dynamic way.

    Look e.g. at http://wiki.pentaho.com/display/EAI/JavaScript+Step+FAQ ... in the "How to check for the existence of fields in rows" section.

    Regards,
    Sven

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.