Hitachi Vantara Pentaho Community Forums
Results 1 to 7 of 7

Thread: JavaScript does not respect length property

  1. #1
    Join Date
    Sep 2008
    Posts
    4

    Default JavaScript does not respect length property

    I have a Java Script that will not respect the length property on a string. It works fine when I click the Test Script button, but when I run it in the job length produces strange results. I am writing the output of the length property into a database column and it yields the following: “function length() {/* int length() */} “. It should yield a simple integer. Why does this tool not respect the length property? Is there something else that I should be using to find the length of a string?
    Here is the script:
    //Script here
    oname = NAME.getString();
    //------------------------------------

    var commaInd = indexOf(oname,",");
    var spaceInd = indexOf(oname," ");
    if (commaInd != -1) {
    lname = oname.substring(0,commaInd);
    //Alert("Input : [" + lname + "]");
    if (spaceInd != -1) {
    var varlen = oname.length;
    Alert("value: " + varlen);
    if (commaInd < varlen) {
    fname = oname.substring(commaInd + 1, spaceInd);
    Alert("fname: " + fname);
    } else { fname = "value: " + varlen }
    if (spaceInd < varlen) {
    mi = oname.substring(spaceInd, varlen);
    Alert("mi: " + mi);
    } else { mi = "value: " + varlen; }
    //var len = oname.length;
    //mi = oname.substring(spaceInd, len);
    //Alert("Input : [" + fname + "]");
    //Alert("Input : [" + mi + "]");
    //fname = oname;
    } else {
    //fname = oname.substring(commaInd, oname.length);
    mi = " ";
    }

    } else {
    lname = oname;
    fname = oname;
    mi = oname;
    newname = oname;
    }

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

    Default

    Use the method length, not ".length" ... it's a "problem" with the integration of the engine.

    Regards,
    Sven

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

    Default

    Not really, oname is a Java string.
    If you turn off compatibility, the oname String is exposed directly and will be available as an embedded JavaScript string (native object).

    That's all there is to it. If you have a String called foo you have 2 variations:

    Code:
    var bar = foo.length
    or in compatible mode:

    Code:
    var bar = foo.getString().length()
    Take care,
    Matt

  4. #4
    Join Date
    Sep 2008
    Posts
    4

    Default change yields error

    I changed "var varlen = oname.length;" to var "varlen = oname.getString().length();" and tried it with compatibility mode checked and unchecked.

    Either way it yields the following error:

    org.pentaho.di.core.exception.KettleException:
    General error executing script:
    TypeError: Cannot find function getString. (script#2)

    at org.pentaho.di.ui.trans.steps.scriptvalues_mod.ScriptValuesModDialog.test(ScriptValuesModDialog.java:1103)
    at org.pentaho.di.ui.trans.steps.scriptvalues_mod.ScriptValuesModDialog.access$300(ScriptValuesModDialog.java:128)
    at org.pentaho.di.ui.trans.steps.scriptvalues_mod.ScriptValuesModDialog$4.handleEvent(ScriptValuesModDialog.java:461)
    at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
    at org.pentaho.di.ui.trans.steps.scriptvalues_mod.ScriptValuesModDialog.open(ScriptValuesModDialog.java:555)
    at org.pentaho.di.ui.spoon.delegates.SpoonStepsDelegate.editStep(SpoonStepsDelegate.java:124)
    at org.pentaho.di.ui.spoon.Spoon.editStep(Spoon.java:6433)
    at org.pentaho.di.ui.spoon.trans.TransGraph.editStep(TransGraph.java:2153)
    at org.pentaho.di.ui.spoon.trans.TransGraph.access$100(TransGraph.java:116)
    at org.pentaho.di.ui.spoon.trans.TransGraph$4.mouseDoubleClick(TransGraph.java:312)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
    at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
    at org.pentaho.di.ui.spoon.Spoon.readAndDispatch(Spoon.java:873)
    at org.pentaho.di.ui.spoon.Spoon.start(Spoon.java:5639)
    at org.pentaho.di.ui.spoon.Spoon.run(Spoon.java:5735)
    at org.pentaho.di.ui.spoon.Spoon.main(Spoon.java:371)
    Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot find function getString. (script#2)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3229)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3219)
    at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3235)
    at org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3247)
    at org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3307)
    at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:1991)
    at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:2932)
    at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2251)
    at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:161)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:340)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2758)
    at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:172)
    at org.pentaho.di.ui.trans.steps.scriptvalues_mod.ScriptValuesModDialog.test(ScriptValuesModDialog.java:1036)
    ... 21 more

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

    Default

    Please Ken, keep the discussion on the forum.

    length sample.ktr

    HTH,
    Matt

  6. #6
    Join Date
    Sep 2008
    Posts
    4

    Default still does not work

    here is my code:

    //Script here
    oname = NAME.getString();
    //------------------------------------

    var commaInd = indexOf(oname,",");
    var spaceInd = indexOf(oname," ");
    if (commaInd != -1) {
    lname = oname.substring(0,commaInd);
    Alert("Input : [" + lname + "]");
    if (spaceInd != -1) {
    var varlen = oname.getString().length();
    //var varlen = oname.length;

    Alert("value: " + varlen);
    if (commaInd < varlen) {
    fname = oname.substring(commaInd + 1, spaceInd);
    Alert("fname: " + fname);
    } else { fname = "value: " + varlen }
    if (spaceInd < varlen) {
    mi = oname.substring(spaceInd, varlen);
    Alert("mi: " + mi);
    } else { mi = "value: " + varlen; }
    //var len = oname.length;
    //mi = oname.substring(spaceInd, len);
    //Alert("Input : [" + fname + "]");
    //Alert("Input : [" + mi + "]");
    //fname = oname;
    } else {
    //fname = oname.substring(commaInd, oname.length);
    mi = " ";
    }

    } else {
    lname = oname;
    fname = oname;
    mi = oname;
    newname = oname;
    }

    When I use the Java syntax and have the Compatibility Mode checked, I get the "TypeError: Cannot find function getString. (script#12)" error, when I use the javascript syntax I get the strange output.

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

    Default

    It's not quite the same is it?

    Code:
    oname = NAME.getString();
    var varlen = oname.getString().length();
    So you're basically getting the length of the String of a String of a Kettle field value? Mmmm.

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.