Just change params.app.workspace.getActiveFile() to app.workspace.getActiveFile()
params is the parameter that the function accepts. Parameters are passed to a function in order to be used inside the function declaration.
function double(number) {
return number + number
}
double(2) // returns 4
Here number is a parameter. It is added to itself and returns the result.
In the case of templater, you pass in arguments* when calling the command:
<% tp.user.double(2) %>
Because you aren’t passing in params it is undefined. Which is why you see an error: “can’t read app of undefined” when try to run params.app....
As to why this works in QuickAdd, my guess would be that QuickAdd is automatically passing the correct argument as params into the function.
Since app is globally scoped, you shouldn’t need to pass it in. Which is why app.metadataCache.getFileCache(activeFile) works later in the code execution.
For more info on functions see: Functions - JavaScript | MDN
* fun fact: when creating the function you define parameters, but when calling the function you pass in arguments. Many people use these words interchangeably and you can essentially think of them as defining the same thing (stuff used inside a function).