console.log in Javascript action/file

0
Hello, It looks like console.log(...) does not show the given message on Javascript actions/files but console.error(...) does. Javascript action in Mendix : function RequireExternalModule() { // BEGIN USER CODE let val; require(["test1"], function (test2) { val = test2; }); console.log("Log in js action"); console.error("Error in js action"); return val.hello(); // END USER CODE } Javascript file in the theme folder of my project : let val = "HelloWorld"; define("test1", [], function () { var myModule = { hello: function() { console.log("Log in js file"); console.error("Error in js file"); return val; } } return myModule; }); When I trigger the Javascript action (using a nanoflow linked to a nanoflow button) I get this result : Am I doing something wrong here ? Thank you !
asked
1 answers
1

Hi Remi,

You are thinking synchronous, while writing asynchronous code. This is the tricky part of Javascript

https://blog.bitsrc.io/understanding-asynchronous-javascript-the-event-loop-74cd408419ff

you have to scope your access of you code correctly, like:

function RequireExternalModule() {
	// BEGIN USER CODE
    let val;
    console.log("Before");
    require(["test1"], function (test2) {
        console.log("Callback");
        val = test2;
        console.log("Log in js action");
        console.error("Error in js action");
        return val.hello();
    });
    console.log("After");
	// END USER CODE
}

Cheers, Andries

answered