Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
Why is a function provided by require undefined in a class instance?

I'm asking this mostly as a sanity check, I'm unsure if I have made a mistake somewhere or if this fundamentally does not work.

I am converting some Java to Nodejs, A class called Site populates itself from a database via the dataFunctions.getSiteInfo(id) function. It also stores a list of Page instances.

The issue is that dataFunctions is returning as undefined when an instance of Site is created.

Have I misunderstood how classes and modules work in this case? Is it possible for instances of this class to access the dataFunctions module? Likewise is it possible for the Site class to reference the Page class as well? Or have I made some other silly mistake?

Site.js

let Page = require('./Page.js');
let dataFunctions = require('../DataFunctions.js');

module.exports = class Site {
    constructor(id) {
        this.id = id;
        this.pages = [];

        console.log(dataFunctions);
        this.siteInfo = dataFunctions.getSiteInfo(id);

DataFunctions.js

let Site = require('./classes/Site.js');

function makeASite() {
    let id = 2;
    let site = new Site(id);
}

function getSiteInfo(id) {
    etc etc
}

module.exports = {
    getSiteInfo: function (id) {
        return getSiteInfo(id);
    },
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on the code provided, I believe you forgot to create a new dataFunctions instance.

const df = new dataFunctions();
console.log(df);

If dataFunctions is providing static functions, make sure that the getSiteInfo method is static.

Otherwise, additional details may be needed.

Edit

It looked like the project structure and the require paths were wonky, but it's hard to tell.

When I run the code provided using the project structure inferred by the require statements, I'm not able to reproduce your issue.

However, one problem that I do see is that you're requiring the Site.js module from the DataFunctions.js module and requiring the DataFunctions.js module from the Site.js module, creating circular dependencies. Try eliminating these by moving makeASite to the Site.js module, for instance.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda