Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
Why is a class member undefined after apparently being assigned a value in constructor?

Why is the class member undefined after definitely being assigned a value in constructor?

main.js:

import { createServer } from "http";
class DefaultHandler {
    handle(i, o) {
        console.log("entered handler");
        o.end(() => {
            console.log("ended");
        });
    }
}
class Gateway {
    handler;
    constructor() {
        this.handler = new DefaultHandler();
        if (this.handler !== undefined)
            console.log("handler instantiated");
    }
    handleTraffic(i, o) {
        console.log("gateway got traffic");
        this.handler.handle(i, o);
    }
}
const server = createServer(new Gateway().handleTraffic);
server.listen({ port: 28751 }, () => {
    console.log(`listening on ${Object.values(server.address()).join(":")}`);
});

then, while also curling localhost:28751, STDOUT is:

$ node --version && node main.js
v16.14.2
handler instantiated
listening on :::IPv6:28751
gateway got traffic
file:///main.js:19
        this.handler.handle(i, o);
                     ^

TypeError: Cannot read properties of undefined (reading 'handle')
    at Server.handleTraffic (file:///main.js:19:22)
    at Server.emit (node:events:526:28)
    at parserOnIncoming (node:_http_server:951:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!