Eggon
Eggon

Reputation: 2356

Property value does not get applied via object literal definition

I have the most bizzare problem. I have a module that aggregates other imported modules and exports them in a single object. All objects from one of the imports (from Person module) do not get applied to their corresponding properties of the 'modules' object. Objects from other imports do get applied properly.

import { Person, MiniPerson } from './Person';
import { Project, MiniProject } from './Project';

export const models = {
    Project: Project, // does get applied properly
    MiniProject: MiniProject, // does get applied properly
    Person: Person, // does not get applied
    MiniPerson: MiniPerson // does not get applied
};

function foo () {
    console.log(models); // returns {
        Project: someValue, MiniProject: someValue, 
        Person: undefined, MiniPerson: undefined }
    console.log(models.Person); // returns undefined
    console.log(Person); // returns a value (import is fine)
    models.Person = Person;
    console.log(models.Person); // returns a value
}

Remarks:

1) All imported objects are analogical and assigned to the aggregating model in the same way, I never had any problems with importing them in other modules (also the ones from Person module).

2) Objects form Person import seem to be imported properly - when I run the foo function they have a value, but the corresponding property of 'models' is undefined. However, the property itself does exist (when logging the 'models' object (or Object.keys(models)), but its value is just undefined!

3) If I assign the property again to the 'models' object in the foo function it does get assigned just fine and no longer returns undefined.

4) I tried restarting computer, creating new file, but it didn't change anything.

What can be the reason? Where else to look? I can use some 'init' function as workaround, but I'd like to avoid this.

Upvotes: 0

Views: 24

Answers (1)

Josh Rutherford
Josh Rutherford

Reputation: 2723

The most likely cause for this sort of behavior is a circular dependency between this module and the Person module. The general way of fixing this is to extract the shared dependency into a separate file.

Upvotes: 1

Related Questions