NodeJS 使用J2V8时(需要)出错

vltsax25  于 2022-12-18  发布在  Node.js
关注(0)|答案(1)|浏览(224)

我尝试使用Ripple-lib来获取客户端的余额,在我的java应用程序中。
为了调用涟漪javascript API,我使用j2v8,如下所示:

V8 runtime = V8.createV8Runtime();
    runtime.executeVoidScript(""
            + "const RippleAPI = require('ripple-lib').RippleAPI;\n"
            + "const api = new RippleAPI({\n"
            + "    server: 'wss://s.altnet.rippletest.net:51233'\n"
            + "});\n"
            + "var client = {\n"
            + "    getBalance: function (address) {\n"
            + "        api.connect().then(() => {\n"
            + "            api.getBalances(address).then(balances => {\n"
            + "                return JSON.stringify(balances, null, 2);\n"
            + "            });\n"
            + "        })\n"
            + "    }\n"
            + "};");
    V8Object client = runtime.getObject("client");
    V8Array parameters = new V8Array(runtime);
    parameters.push("rHY6yUsQaEigs867XUgaMp89Hhm2eJs5jQ");
    String result = client.executeStringFunction("getBalance", parameters);
    System.out.println(result);
    parameters.release();
    runtime.release();

但有一个例外

Exception in thread "main" undefined:1: ReferenceError: require is not defined
const RippleAPI = require('ripple-lib').RippleAPI;
                  ^
ReferenceError: require is not defined
    at <anonymous>:1:19
com.eclipsesource.v8.V8ScriptExecutionException

有人能帮帮我吗?

bzzcjhmw

bzzcjhmw1#

如果你运行的是j2v8,引擎只知道纯js,甚至不知道窗口变量,因为窗口、文档和Dom操作API是由浏览器提供的,而不仅仅是引擎。需要的不是js函数。它不会被v8引擎识别

相关问题