Skip to content

TypeError: can't convert undefined to object #2595

Closed
@SimonChaumet

Description

Intended outcome:

I want to access my root store from my children stores without passing the root store argument all the way down.

Actual outcome:

I get TypeError: can't convert undefined to object in the console with no stacktrace as soon as I add my code to access the root store.

How to reproduce the issue:

// root store
export class StoreRoot {
    catalog: StoreCatalog;

    constructor(catalogData: IRCatalog) {
        makeAutoObservable(this);
        this.catalog = createStoreCatalog(catalogData);
    }
}

export const ROOT_STORE = createRootStore();

function createRootStore(): StoreRoot {
    return new StoreRoot(catalogData);
}

// child store in separated file
export class Theme implements ITheme {
    themeId: number;
    title: string;
    _contexts: Context['contextId'][]

    constructor(theme: ThemeConstructorOptions) {
        makeAutoObservable(this);
        this.themeId = theme.themeId;
        this.title = theme.title;
        this._contexts = theme.contexts;
    }

// This function is the code I want
    /* get contexts(): IContext[] {
        return this._contexts.map(contextId => ROOT_STORE.catalog.contexts[contextId]);
    }*/

    get contexts(): IContext[] {
        console.log(ROOT_STORE); // When adding this line I get the error
        return [];
    }
}

Versions

  • MobX 6.0.3
  • MobX react 6.3.1
  • React Native 0.63.3
  • React 16.31.1

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions