Page 1 of 1

Creating native Java Objects in Javascript 2x Server Extension.

Posted: 04 May 2021, 07:06
by cevans
This is probably a simple question / answer, but after searching the forum I couldn't find the answer.

I'm using Javascript extensions with the SFs2X server and I'm trying to run the dbManager method, executeInsert(). The problem is the second parameter, it's expecting a Ljava.lang.Object. I don't how to cast that from Javascript. I tried using a SFSObject and still ran into casting issues. This is part of the stack error:

+--- --- ---+
Stack Trace:
+--- --- ---+
java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
jdk.nashorn.internal.scripts.Script$Recompilation$236$991$db_utilities.testInsert(extensions/polimonExt//util/db_utilities.js:58)
jdk.nashorn.internal.scripts.Script$Recompilation$231$55$\^eval\_.init(<eval>:9)
jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:199)
jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:386)
jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:190)
com.smartfoxserver.v2.extensions.JavascriptExtension.invokeFunction(JavascriptExtension.java:227)
com.smartfoxserver.v2.extensions.JavascriptExtension.init(JavascriptExtension.java:61)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.createExtension(SFSExtensionManager.java:303)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.reloadExtension(SFSExtensionManager.java:534)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager$ExtensionFileChangeListener.fileChanged(SFSExtensionManager.java:111)


The DbManager is accessible through Javascript extensions, but how to use DbManager methods where the parameters require native Java objects? This applies to other Java classes that are accessible in JS extensions.

Any help would be appreciated!

Re: Creating native Java Objects in Javascript 2x Server Extension.

Posted: 04 May 2021, 08:07
by Lapo
Hi,
the underlying Java method takes two parameters: a String with the SQL expression and Object[] (object array) with parameters, for each placeholder you have specified in the SQL expression.

Here's a query Example, the same can be done for an insert or update operation:

Code: Select all

function init()
{
	trace("[JS DB] Extension started");
	var dbMan = getParentZone().getDBManager();

	var sql = "SELECT * FROM people WHERE location=? OR location=?"
	var res = dbMan.executeQuery(sql, ['Italy','Austria'])

	trace(res.getDump())
}
Hope it helps

Re: Creating native Java Objects in Javascript 2x Server Extension.

Posted: 04 May 2021, 10:57
by cevans
Great thanks. So I can just pass the Javascript datatypes. I made the mistake of passing an object instead of an array.