Inside my application I've created a number of ES6 "classes" in a way that each defines it's properties together with custom setters, inside of which - before assigning the property - it's running a field-specific validator. Now, as models may have the same fields available and I don't want to duplicate the code of validators, I planned to import models as needed and just reuse validators as I'm exposing them via class' static property (check the code for better understandig). Unfortunately this leads to circular dependencies and as a results - breaks the whole thing.
I understand what is happening and why, but I can't find a way to solve it. I tried answers provided on stack and went through a number of articles from Google but without luck.
I tried moving exports
before require
, declaring my structures as var
to use hoisting, declare empty structures before exports
and define them after and many, many more.
I know I can solve this case by moving common validators to a separate file and import it in both modules but that's not what I'm looking for as this is not exactly solving the problem for the future as I may need other properties/methods to be exposed.
Plus - it just doesn't seem right: ClassA properties' validators should stick with ClassA "domain" and vice versa.
Now the code. This is simplified, but working version of what I'm using:
// ClassA.js:
const { ClassB } = require('./ClassB');
// This prints: ClassA inside ClassB: undefined
console.log('ClassB inside ClassA:', ClassB);
const _validators = {
// This is validator defined in ClassA...
property_a: (value) => true,
// ...And this one is reusing a validator from ClassB
// I'm getting: TypeError: Cannot read property 'VALIDATORS' of undefined
property_b: ClassB.VALIDATORS.property_b,
};
exports.ClassA = class ClassA {
constructor() {
// Here comes the code defining descriptors for each property
// and running the property-specific validator inside
// a field setter
}
// Expose class-specific validators as class property
static get VALIDATORS () {
return _validators;
}
};
// ClassB.js:
const { ClassA } = require('./ClassA');
console.log('ClassA inside ClassB:', ClassA);
const _validators = {
// This is validator defined in ClassA...
property_a: ClassA.VALIDATORS.property_a,
// ...And this one is reusing a validator from ClassB
property_b: (value) => true,
};
exports.ClassB = class ClassB {
constructor() {
// Here comes the code defining descriptors for each property
// and running the property-specific validator inside
// a field setter
}
// Expose class-specific validators as class property
static get VALIDATORS () {
return _validators;
}
};
// app.js
const { ClassA } = require('./ClassA');
const { ClassB } = require('./ClassB');
console.log(ClassA, ClassB);
Because of circular imports ClassA inside ClassB is undefined
(although according to articles I've read it should be {}
) what is visible on console.log
and causing the error I'm getting:
TypeError: Cannot read property 'VALIDATORS' of undefined
at Object.<anonymous> (/repository/ClassB.js:6:24)
How can I redefine/rearrange the code to get this working, without creating another, common file?
Any help would be appreciated.
I'm using: Node.js v.11.6.0
ClassA
properties' validators should stick withClassA
"domain" and vice versa." - well yes, it definitely doesn't seem right thatClassA
exposesClassB.VALIDATORS.property_b
and vice versa. Why are you doing this? – Bergi Jan 16 '19 at 9:27