简体   繁体   English

类型错误:找不到 function 等等

[英]TypeError: Cannot find function blah in

I'm using the JavaScript support in javax.script to do some basic unit testing of JS scripts and I've run into a problem.我在 javax.script 中使用 JavaScript 支持对 JS 脚本进行一些基本的单元测试,但遇到了问题。

I have some code that eval(...) s the pre-requisite lib files I have in an instance of ScriptEngine, including the one that has my function blah in it.我有一些代码eval(...)是我在 ScriptEngine 实例中拥有的先决条件库文件,包括其中包含我的 function blah 的那个。

I then eval a further file in the same instance of ScriptEngine, in which I have some functions that serve as tests, and then I invoke the tests.然后我在 ScriptEngine 的同一个实例中评估另一个文件,其中我有一些用作测试的函数,然后我调用测试。

However, despite all the evals working without issue, I get an error that to me suggests a problem loading the function.然而,尽管所有的评估工作没有问题,我收到一个错误,对我来说表明加载 function 时出现问题。

javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function blah. (<Unknown source>#x) in <Unknown source> at line number x

Two separate eval calls on the same ScriptEngine works for me.同一个ScriptEngine上的两个单独的eval调用对我有用。 Are you setting a new ScriptContext or otherwise eliminating your definition of blah ?您是在设置新的ScriptContext还是以其他方式消除对blah的定义?

public class TryScripting {
    public static void main(String[] args) throws ScriptException {
    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
    String makeFun = "function  hello() {\n" +
            "return \"hello world\";\n" +
            "}\n" +
            "{\n" +
            "}";
    engine.eval(makeFun);

    engine.eval("myVar = hello()");
    ret = engine.get("myVar");
    System.out.println(ret);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:在对象中找不到函数getElementById - TypeError: Cannot find function getElementById in object Java“找不到主类”等等,但是有些奇怪: - Java “Could not find main class” blah blah blah, but something strange: 类型错误:在 object function Object() 中找不到 function 条目 - TypeError: Cannot find function entries in object function Object() JNA无法找到功能 - JNA cannot find function 从JavaScript调用@ JSFunction,TypeError:找不到对象的默认值 - Calling @JSFunction from JavaScript, TypeError: Cannot find default value for object 试图获得不同的结果,说不能将字符串强制转换为blah.Model.Article - Trying to get distinct results, saying string cannot be cast to blah.Model.Article 无法通过Rserve查找功能,但在R中可见 - Cannot find function via Rserve but it is visible in R android找不到对应的jni函数 - android cannot find corresponding jni function 我不能使用地图函数 TypeError:无法读取未定义的属性“地图” - i cant use the map function TypeError: Cannot read property 'map' of undefined IBM Worklight 6.1-“ Ecma错误:TypeError:无法调用属性 <function> 在对象中” - IBM Worklight 6.1 - “Ecma Error: TypeError: Cannot call property <function> in object”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM