Send js variable to App variable

I tried to assign a js variable to a AC variable …works with hard value, bu not with js variable

browservalue = dmx.parse('lang.value'); // store 'fr'
alert(browservalue);  //display 'fr'
dmx.parse("sendlangtosession.setValue('hard value ok');");
dmx.parse("sendlangtosession.setValue(browservalue);");  //not ok

Error is :

SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at sessionManager.js:28
    at Array.reduce (<anonymous>)
    at n.getData (sessionManager.js:26)
    at n.set (sessionManager.js:12)
    at Object.data.<computed> (BaseComponent.js:475)
    at parser.js:714
    at t (parser.js:464)
    at parser.js:455
    at Object.dmx.parse (parser.js:388)

Is it possible be that when that first variable browservalue is set, that the app connect variable Lang hasn’t filled loaded in the DOM?

I know from my experiences using JS in Wappler I always need to either call a JS function on an event in Wappler (I.e. button click or onload for a server action) or use other trigger events like document ready/onload etc.

do you have code sample ?

Sorry no, not at my PC. But test it by changing the browservalue to a hard value.
var browservalue = 'testValue'

this is what i did in this code… its working… only with a variable not working

Or wrap it all in a function and call the function on a button in your app to test.

If it works it’s probably just a timing issue and the app connect variable just hasn’t loaded.

You can google something like JavaScript getelememtbyid document ready

In fact it is already in a ready function

<script type="text/javascript">
		$(document).ready(function () { 
		browservalue = dmx.parse('lang.value'); 
                alert(browservalue); 
    		dmx.parse("sendlangtosession.setValue('hard value ok');");
		dmx.parse("sendlangtosession.setValue(browservalue);");
		});
</script>

Ok well I’m not too sure then. But it could still be worth testing a few things. The functions definitely work.

Not sure if it makes a difference but I think I read somewhere that dmx.parse really should use single quotes not double quotes inside the brackets.

I also never have a semicolon inside my dmx.parse but don’t know of it makes a difference.

The browservalue variable is a JS variable. When you pass it to dmx.parse inside double quotes, it assumes that its a DMX object, which does not exists. Hence the value that gets set in the session is undefined. Which leads to the error your are seeing… unexpected token u.

Correct code would be:
dmx.parse("sendlangtosession.setValue('" + browservalue + "')");

3 Likes

Perfect its working thanks !