[tw5] Need help to create a javascript library & invoke this.wiki.xxxx() from it

Hi guys,

I need a bit of help to understand how to access getTiddler method from a javascript library

When I develop macro (module-type) I use this.wiki. filterTiddlers (…) methods and all works fine when I invoke this macro from a tiddler script

But I would like to develop a javascript library that is intended to be used by different macros. This library will need to access a Data Tiddler (as a DB “backend”)

So I created a javascript code and set its module-type to library
The code is the following

(function(){
class Hero {
constructor(name, level) {
var stats_tiddler_title = “Stats_report”; // a backend tiddler that contains the stats reports (as field value)
var stats_tiddler_filter = “[[”+stats_tiddler_title+"]]";
var stats_tiddlers = this.wiki.filterTiddlers(stats_tiddler_filter);
this.name = name;
this.level = level;
console.log(" Class created. Value:"+this.level);
}

getName() {
console.log(" getName !!!. Name:"+this.name);
}
}
exports.Hero = Hero;

})();

var Hero = require(’$:/plugins/vpl/vpl_test_class.js’).Hero;

const hero1 = new Hero(‘Varg’, 1);

But I get the error
Uncaught TypeError: Cannot read properties of undefined (reading ‘filterTiddlers’)

I’m blocked … Can someone help & guide me on the proper way to do that ?

Thanks very much for your help

Regards

Try using $tw instead of this:

var stats_tiddlers = $tw.wiki.filterTiddlers(stats_tiddler_filter);

-e

Fantastic !!
I’ve tested this option previously but outside the (function(){. This was not working.
But inside the function this works

Thanks a lot