console.log not working in use function | Community
Skip to main content
Level 3
September 16, 2022
Solved

console.log not working in use function

  • September 16, 2022
  • 3 replies
  • 3883 views

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.
Best answer by Sachin_Arora_

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

ksh_ingole7
Community Advisor
Community Advisor
September 17, 2022

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

Saravanan_Dharmaraj
Community Advisor
Community Advisor
September 17, 2022

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.

Sachin_Arora_
Community Advisor
Sachin_Arora_Community AdvisorAccepted solution
Community Advisor
September 18, 2022

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.