We have a 5 years old nodejs source code which must be upgraded to the latest nodejs technology, packages, typescript compiler. We found a piece of code which uses the moleculer
nodejs package, and a code looks as:
broker.createService({
name: 'file',
actions: {
save: {
handler(ctx) {
return new this.Promise((resolve, reject) => {
const filePath = path.join(__dirname, 'public/upload', ctx.meta.filename);
const f = fs.createWriteStream(filePath);
f.on("close", () => {
// !!! calling this.logger !!!!
this.logger.info(`Uploaded file stored in '${filePath}'`);
resolve(filePath);
});
f.on("error", err => reject(err));
ctx.params.pipe(f);
});
}
}
}
})
As per code line this.logger.info()
the typescript compiler says:
error TS2339: Property 'logger' does not exist on type ...
How to handle this error?
Also: we can add actions
and methods
to this service:
methods: {
createQueueHandler(queueName, handler) { ... {
}
but calling this on an action
created() {
this.logger.info(`RabbitMQService CREATED`);
this.createQueueHandler(pdfConverterCompletedQueue, pdfCompleteHandler.bind(this));
},
also causes compiler error:
Error TS2339: Property 'createQueueHandler' does not exist on type
It is a different problem - how to handle this?
"moleculer": 0.14.29, typescript: 5.1.6, and tsconfig:
{
"compilerOptions": {
"noImplicitAny": false,
"strictNullChecks": false,
"esModuleInterop": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"lib": [ "esnext", "dom" ],
"target": "es2015",
"strict": true,
"outDir": "./build/src",
"preserveConstEnums": true,
"removeComments": true,
"sourceMap" :true,
"typeRoots": [ "./node_modules/@types" ],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types" : [ "node" ],
},
"include": ["src/**/*", "test/**/*"],
"exclude": [ "node_modules", "**/*.integration.ts" ]
}