Running the example user script from Templates returns "undefined"

Things I have tried

I have looked at similar topics on this forum and the Templater documentation, but cannot find the solution to my problem.

What I’m trying to do

I am trying to run the example User Script from Templater, where I create the following javascript:

function my_function (msg) {
    console.log("Message from my script:", msg);
}
module.exports = my_function;

which is detected by Templater as user script. However, when calling this script using:

<% tp.user.my_script("Hello World!") %>

instead of the expected “Message from my script: Hello world!”, I get: “undefined”.

Hello! I also am just learning to script, so I’m afraid I can’t help you more than this, but I see one and a half problems:

  1. You have to return the string if you want it to be called and
  2. You have to name the script the same things as you call it. As far as I know the function name has to correspond to the filename, so if the filename matches than you can ignore this point, but as you make more scripts it may be helpful to have consistent naming.

Changing your script to the following should give you your expected results.

function my_function (msg) {
    return "Message from my script:" + msg;
} module.exports = my_function

Best wishes!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.