While compiling there are no errors, compile fine. But when I call the method in the controller got an error. It looks like during the compilation class methods are missing.
If you need more information about the project config - please, ask - I will share. For now, I don't have any ideas on how to fix it. The similar code on other projects works fine.
TypeError: Cannot read properties of undefined (reading 'onOpenMessage') at WebSocket. (/Users/xxx/redqueen-middleware/src/controllers/websocket.ts:23:16) at WebSocket.onMessage (/Users/xxx/redqueen-middleware/node_modules/ws/lib/event-target.js:132:16) at WebSocket.emit (node:events:513:28) at Receiver.receiverOnMessage (/Users/xxx/redqueen-middleware/node_modules/ws/lib/websocket.js:1068:20) at Receiver.emit (node:events:513:28) at Receiver.dataMessage (/Users/xxx/redqueen-middleware/node_modules/ws/lib/receiver.js:517:14) at Receiver.getData (/Users/xxx/redqueen-middleware/node_modules/ws/lib/receiver.js:435:17) at Receiver.startLoop (/Users/xxx/redqueen-middleware/node_modules/ws/lib/receiver.js:143:22) at Receiver._write (/Users/xxx/redqueen-middleware/node_modules/ws/lib/receiver.js:78:10) at writeOrBuffer (node:internal/streams/writable:391:12) [ERROR] 16:46:06 TypeError: Cannot read properties of undefined (reading 'onOpenMessage')
Controller code
class WebsocketController {
initWebsocket(websocket: WebSocket): void {
//some code, works fine before calling method `onOpenMessage`
this.onOpenMessage();
}
private onOpenMessage(): void {
//some code
}
}
export default new WebsocketController();
My tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"esModuleInterop": true,
"lib": [
"dom",
"es6",
"esnext.asynciterable"
],
"typeRoots": [
"./custom_typings",
"./node_modules/@types"
],
"sourceMap": true,
"allowJs": true,
"outDir": "./build",
"baseUrl": "./src",
"resolveJsonModule": true
},
"include": [
"src/**/*",
"config/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts",
"**/*.test.ts",
"custom_typings"
]
}
Here is my server.ts
import express from 'express';
import expressWs from 'express-ws';
import winston from 'winston';
import WebsocketController from './controllers/websocket';
export default function initServer() {
const app = express();
const port = process.env.PORT;
const router = express.Router();
global.websocketsList = [];
expressWs(app);
router.ws('/ws-request', WebsocketController.initWebsocket);
app.use('/', router);
app.listen(port, () => {
winston.info(`Middleware server start on port ${port}`);
});
}
Converting method onOpenMessage
from private to public does not give any result.
CodePudding user response:
You are trying to use the instance method as statis
You lost
this
reference
Create an instance and use anonymous function instead:
const wsController = new WebsocketController()
router.ws('/ws-request', () => wsController.initWebsocket());
CodePudding user response:
You never instantiate the class. Should
const wsController = new WebsocketController()
router.ws('/ws-request', wsController.initWebsocket);
Or:
Export the instance from the controller file:
exports.default = new WebsocketController()