Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App works, but DevTools gives error "TypeError: this.todos is undefined" #75

Open
darkdark87 opened this issue Oct 26, 2017 · 3 comments

Comments

@darkdark87
Copy link

darkdark87 commented Oct 26, 2017

The setup is simple. React with MobX and TypeScript along with MobX DevTools. The app is pretty much a copy paste of the 10 min todo introduction of MobX. The app works, but once I try to inspect the content of my store with DevTools I receive an error:

image

The code for the store is as follows:

export default class ObservableTodoStore {
    @observable 
    todos: any[] = [];
    @observable 
    pendingRequests: number = 0;

    constructor() {
        autorun(() => console.log(this.report));
    }

    @computed 
    get completedTodosCount() {
        return this.todos.filter(
            (todo: any) => todo.completed === true
        ).length;
    }

    @computed 
    get report() {
        if (this.todos.length === 0) {
            return '<none>';
        }
        return `Next todo: "${this.todos[0].task}". ` +
            `Progress: ${this.completedTodosCount}/${this.todos.length}`;
    }

    @action
    addTodo(task: any) {
        this.todos.push({
            task: task,
            completed: false,
            assignee: null
        });
    }
}
@tonycaputome
Copy link

tonycaputome commented Jan 3, 2018

I have the same problem everytime when I try to inspect the store.

@tonycaputome
Copy link

I solved the issue, my problem was using utility functions of an observable array ( toJS, slice() ...) in a computed property

class Store {
@observable users;

// init the observable in the constructor
constructor() {
  this.users = observable.array([]);
}

// some actions here that mutate your observable ...

// generate a console error
@computed get users() {
   return this.users.slice(); // or toJS()
}

// works
@computed get users() {
   return this.users;
}

}

@mweststrate
Copy link
Member

Hard to say anything useful about this one without a reproduction project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants