Require resolves undefined

Gleb Oleg

I have the following class structure.

EmployeeService.js

const {EmployeeRequestExecutor} = require('./EmployeeRequestExecutor');

class EmployeeService {
    fulfill(request){
        EmployeeRequestExecutor.delegateEmployeeRequest(request);
    }
}

module.exports = {EmployeeService};

EmployeeRequestExecutor.js

const {HelpfulEmployee} = require('./HelpfulEmployee');

class EmployeeRequestExecutor {
    static delegateEmployeeRequest(request){
        let employee = new HelpfulEmployee();
        employee.work(request);
    }

    static executeEmployeeRequest(request){
        console.log('Executed', request);
    }
}

module.exports = {EmployeeRequestExecutor};

HelpfulEmployee.js

const {EmployeeRequestExecutor} = require('./EmployeeRequestExecutor');

class HelpfulEmployee {
    work(request){
        EmployeeRequestExecutor.executeEmployeeRequest(request);
    }
}

module.exports = {HelpfulEmployee};

If we then do

let employeeService = new EmployeeService();
employeeService.fulfill(request);

we get TypeError: Cannot read property 'executeEmployeeRequest' of undefined in HelpfulEmployee.js:5:33

Why is it so that with such call structure EmployeeRequestExecutor inside HelpfulEmployee.js is undefined?

Jean-Baptiste Martin

There is a circular dependency between HelpfulEmployee and EmployeeRequestExecutor (they both require each other). Circular dependencies result most of the time from class design issues. To fix them, you should reconsider the flow of information between your objects.

This is an option, but there are many more:

const {HelpfulEmployee} = require('./HelpfulEmployee');

class EmployeeRequestExecutor {
    static delegateEmployeeRequest(request){
        let employee = new HelpfulEmployee();
        let requestExecutor = new EmployeeRequestExecutor();
        employee.work(request, requestExecutor);
    }

    executeEmployeeRequest(request){
        console.log('Executed', request);
    }
}

module.exports = {EmployeeRequestExecutor};
class HelpfulEmployee {
    work(request, requestExecutor){
        requestExecutor.executeEmployeeRequest(request);
    }
}

module.exports = {HelpfulEmployee};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Axios POST resolves to undefined

Axios & Redux returns undefined, than resolves

React Native 'require' statement resolves to a number when used in mapping

NodeJS HTML: 'require' is undefined

Node require returns undefined

Undefined variable with include or require

Jest async function always returns undefined, never resolves

C++ why templating a class resolves undefined class error

import qs is undefined, but works with require

Node-webkit require is undefined

Require, Knockout and pager are Undefined TypeError

Require argument if unknown type is not undefined

require('search') is undefined using RequireJS

Handlebars is undefined using Require.js

CasperJS require('utils').dump(this.logs) is undefined

node.js require results undefined

node debug / inspect require returns undefined

Ruby require causing undefined local variable

Require.js/Jquery Datable : undefined is not a function

Dealing with undefined dependencies in require.js

AngularJS and Require: r.js gives undefined

Cannot destructure property 'interface' of 'require(...)' as it is undefined

JS Require statement leaving variable undefined

undefined rspec method, maybe require incorrectly?

require('electron').app is undefined - how to solve this

Meteor : "console" variable undefined inside require call

constructor for class is undefined after require in module

why this resolves to undefined not window/global-env in react component method if it not bind this in constructor

'uri.match' is undefined trying to require audio file into video source