Error in calling JS code from java 2.4.0

Hi, @slackmoehrle @zzf_Cocos while using 1.8.2 my below function is working. but in 2.4.0 it is not working and giving this error

01-10 12:52:19.327 1275-1362/com.org.test E/jswrapper: ScriptEngine::onGetStringFromFile CashierPopUp not found, possible missing file.
    ScriptEngine::runScript script CashierPopUp, buffer is empty!
    [ERROR] Failed to invoke require, location: /Applications/CocosCreator/Creator/2.4.0/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/manual/jsb_global.cpp:300
01-10 12:52:19.328 1275-1362/com.org.test E/jswrapper: ScriptEngine::evalString catch exception:
    ERROR: Uncaught TypeError: Cannot read property 'prototype' of undefined, location: (no filename):0:0
    STACK:
    [0]anonymous@(no filename):1
    ScriptEngine::evalString script (no filename), failed!

and my java function in appActivity is

public void onSuccess(Map<String, String> map) {
        final Object[] successObj = map.entrySet().toArray();
        Log.d("CFSDKSample", "Payment Success");
        Log.d("Payment success values", Arrays.toString(successObj));
        inst.runOnGLThread(new Runnable() {
            @Override
            public void run() {
                Cocos2dxJavascriptJavaBridge.evalString("var CashierPopUp = require ('CashierPopUp'); CashierPopUp.prototype.returnSuccessToServer(\"" + Arrays.toString(successObj) + "\");");
            }
        });
    }

please help

CashierPopUp is a null value that causes the problem.
So there is a problem with the require.

Solution:
Expose a global object via window in the JS script of CashierPopUp and then call this global object.

Hi @muxiandong can you please explain how I can do this, I have the CashierPopUp.js script and I want to require CashierPopUp because there is a function “returnSuccessToServer()” I want to call this function.

You can try to get the CashierPopUpTest property in the CashierPopUp.js script by " window.CashierPopUpTest = xxx" and then call CashierPopUpTest

Thank you for your reply @muxiandong, I have define “window.CashierPopUpInit=this” onLoad().
After that i have put this function on end

//inside cashierPopup.js
window.CashierPopupCall = function(data){
window.CashierPopUpInit.returnSucessToServer(data);
}

And in AppActivity

inst.runOnGLThread(new Runnable() {
            @Override
            public void run() {
                Cocos2dxJavascriptJavaBridge.evalString("CashierPopupCall(\"" + Arrays.toString(successObj) + "\");");
            }
        });

Yes, this is the correct usage

1 Like