console.log not working in use function | Community
Skip to main content
Level 3
Sep 16, 2022 9:08 PM UTC
Solved

console.log not working in use function

I'm trying to write some logic in JavaScript file in my custom component but the use function is not firing the console.log. Does anyone know why it won't work. I even tried using window.alert but that won't work either. but I can create a var and assign a string value and display that value through expression in HTL. 

 

Here is the the JS code. 

use(function () { var props = { test: "test", test2: "test2" }; console.log("----------------Test_---------") return { // anything exposed here can be used inside your template props: props, }; });​
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Sachin_Arora_
Sachin_Arora_Accepted solution
New Participant
Sep 18, 2022 7:24 AM UTC

As you are using Use API, which is actually converted into JAVA instead of loading as a JS in a browser, you have to use JAVA object to log the information. Below code is working fine on my local.

log.info("Using USE API");

Also this log will be generated in server log files instead of browser console, so you have to create a log file of the component you are using for example if your component is /apps/we-retail/components/sample then you have to create a log file of : apps.weretail.components.sample from http://localhost:4502/system/console/slinglog. 

Please update if you need sample working code.

3 replies

Sachin_Arora_
Sachin_Arora_Accepted solution
New Participant
Sep 18, 2022 7:24 AM UTC

As you are using Use API, which is actually converted into JAVA instead of loading as a JS in a browser, you have to use JAVA object to log the information. Below code is working fine on my local.

log.info("Using USE API");

Also this log will be generated in server log files instead of browser console, so you have to create a log file of the component you are using for example if your component is /apps/we-retail/components/sample then you have to create a log file of : apps.weretail.components.sample from http://localhost:4502/system/console/slinglog. 

Please update if you need sample working code.

Translate
Saravanan_Dharmaraj
New Participant
Sep 17, 2022 10:34 PM UTC

This is server side javascript. window.alert() is for client side js.

Try to Add a new logger in AEM and print the statements with log.debug statements 

please check this https://jimfrenette.com/aem/javascript-use-api/ for help.

Translate
ksh_ingole7
New Participant
Sep 17, 2022 8:54 AM UTC

Hi @moiezma 

 

Go to developer tools - > Source Tab -> Find your JS code and place a debug breakpoint on your code. See if the debugger stops at your breakpoints. See if the code is actually executed.

 

Thanks

Translate