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

5.4 Cannot read property 'enumerable' of undefined #191

Closed
timothyallan opened this issue Apr 20, 2019 · 7 comments
Closed

5.4 Cannot read property 'enumerable' of undefined #191

timothyallan opened this issue Apr 20, 2019 · 7 comments
Labels

Comments

@timothyallan
Copy link

Hey all, ViewModel is 5.4 is lighting up my console with waaaay too much red text!

My class objects which I'm using as ViewModels have the usual observable properties, but occasional functions as well.

It seems like this is the culprit

const { enumerable } = Object.getOwnPropertyDescriptor(model, key)

In this loop,

getAllMethodsAndProperties(model).forEach((key: any) => {

As soon as the key is the name of a function in my class object, I get Uncaught TypeError: Cannot read property 'enumerable' of undefined

and everything explodes. 5.2 works great.

@ItamarShDev
Copy link
Member

9aebd22

@ItamarShDev
Copy link
Member

ItamarShDev commented Apr 21, 2019

@timothyallan can you please provide a codesandbox example?
Edit Minimal mobx + react project

@timothyallan
Copy link
Author

Sure, it won't let me save it, but simply paste this into the index.js and it crashes.

import React from "react";
import { render } from "react-dom";
import { observable, action } from "mobx";
import { observer } from "mobx-react";
import DevTools from "mobx-react-devtools";
import { createViewModel } from 'mobx-utils';

class MyClass {
  @observable firstname;
  @observable lastname;

  someFunction(someParam) {

  }
}

class AppState {
  constructor() {
    const myClass = new MyClass();
    const observableMyClass = createViewModel(myClass);
  }

  @action.bound
  reset() {
    this.timer = 0;
  }
}

const TimerView = observer(({ appState }) => (
  <button onClick={appState.reset}>Seconds passed: {appState.timer}</button>
));


render(
  <div>
    <TimerView appState={new AppState()} />
    <DevTools />
  </div>,
  document.getElementById("root")
);

@ItamarShDev
Copy link
Member

Using arrow function seems to work, like someFunction = (someParam) => {}
I am not saying its what should happen, but give my take

your thoughts @mweststrate ?

@mweststrate
Copy link
Member

the arrow function "fixes" it as that makes the method a local (own) property, which is present indeed.

I think a simple fix (strictly speaking not 100% correct) would be:

const descriptor = Object.getOwnPropertyDescriptor(model, key)
const enumerable = descriptor ? !!descriptor.enumerable : false

@dr0p
Copy link
Contributor

dr0p commented May 7, 2019

Please review this fix #198
Sorry for the miss..

@timothyallan
Copy link
Author

This seems fixed now in 5.4.1. Thanks!

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

No branches or pull requests

4 participants