Important: Please read the Qt Code of Conduct -

QJSEngine and SyntaxError ?

  • I am using the QJSEngine, I added a global to the object:

        QJSValue objGlobal = pobjScriptEng->globalObject();

    pobjScriptEng is a pointer to an instance of QJSEngine.

    I have a map of globals, the map type definition:

        std::map<QString, QString> mpGlobals;

    I iterate through the globals map adding them to the engine:

        for( mpGlobals::iterator itr=rmpGlobals.begin(); itr!=rmpGlobals.end(); itr++ ) {
            QString strName(itr->first);
            if ( objGlobal.hasProperty(strName) == false ) {
                QString strData(itr->second);
                QJSValue result = pobjScriptEng->evaluate(strData);
                objGlobal.setProperty(strName, result);

    rmpGlobals is a reference to the globals map.

    I have a script which includes a reference to a global, the global is called "db" and is a JSON object that contains:


    I added some debug to the loop that calls setProperty and this is whats displayed in the Application Output:

        itr->first "db" 
        itr->second "{\"db\":\"test\",\"host\":\"localhost\",\"usr\":\"root\",\"pass\":\"resuocra\"}"

    The syntax error is coming from the JSON, but why there is nothing wrong with it. I dumped result.toString() to the console and it contains:

        SyntaxError: Expected token `,'

    The script:

        function test() {
"test(), Line 4");
        		if ( db === undefined ) {
 "db is undefined");
        		if ( typeof db === "object" ) {
        			var mbr;
 "test(), Line 7");
 "test(), Line 9");
        			for( mbr in db ) {
  "test(), Line12");
  "test(), Line14");
 "test(), Line 14");			
"test(), Line 16");		
        	} catch( e ) {
        		console.warn( "test() WARNING: " + e );

    When run my application and the script is evaluated I see the following in the "Application Output"

        2020-04-08 08:21:36.320693+0100 XMLMPAM[3657:59571] [js] ---------------
        2020-04-08 08:21:36.320732+0100 XMLMPAM[3657:59571] [js] test(), Line 4
        2020-04-08 08:21:36.320747+0100 XMLMPAM[3657:59571] [js] test(), Line 7
        2020-04-08 08:21:36.320762+0100 XMLMPAM[3657:59571] [js] SyntaxError: Expected token `,'
        2020-04-08 08:21:36.320769+0100 XMLMPAM[3657:59571] [js] test(), Line 9
        2020-04-08 08:21:36.320790+0100 XMLMPAM[3657:59571] [js] test(), Line 14
        2020-04-08 08:21:36.320798+0100 XMLMPAM[3657:59571] [js] test(), Line 16
        2020-04-08 08:21:36.320804+0100 XMLMPAM[3657:59571] [js] ---------------

    Ignore everything before the [js] thats my time stamp and debug information, after the [js] is all the console output.

    What is the:

        SyntaxError: Expected token `,'

    I can see nothing wrong in the global or the script.

    If I modify the script and insert:

        var db =  {"db":"test","host":"localhost","usr":"root","pass":"123456"};

    As the first line, the syntax error is not displayed and everything is ok, so what is wrong with global added using setProperty ?

    Here is the code that adds a global to the map:

        void clsXMLnode::addGlobal(QString strGlobal) {
            QStringList slstGlobal = strGlobal.split(clsXMLnode::msccGlobalDelimiter);
            if ( slstGlobal.length() == clsXMLnode::mscintAssignmentParts ) {
                QString strName(slstGlobal[clsXMLnode::mscintGlobalName].trimmed())
                msmpGlobals.insert(std::make_pair(strName, strValue));

    Some definitions:

        clsXMLnode::msccGlobalDelimiter        is "="
        clsXMLnode::mscintAssignmentParts      is 2
        clsXMLnode::mscintGlobalName           is 0
        clsXMLnode::mscintGlobalValue          is 1

  • The solution was to wrap the JSON in (), this was not required in earlier versions and I fail to see why its required now, but it has solved the issue.

  • The solution was to wrap the JSON in (), this was not required in earlier versions and I fail to see why its required now, but it has solved the issue.

  • @SPlatten said in QJSEngine and SyntaxError ?:

    The solution was to wrap the JSON in ()

    Where are you doing this? On the c++ side or the qml side? I seem to remember some weirdness with something like this, but I honestly don't remember where the problem was.

  • In the C++, I'm not using QML in my application.

Log in to reply