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

Cannot read property 'atom' of undefined when I call reduce on an observable array #234

Closed
capaj opened this issue May 3, 2016 · 12 comments
Labels

Comments

@capaj
Copy link
Member

capaj commented May 3, 2016

I just stumbled on this error:

Uncaught TypeError: Cannot read property 'atom' of undefined
Object.defineProperty.value @ mobx.js:1147
priceSum @ orders.js:110
dehydrate @ backend.js:1685
dehydrate @ backend.js:1685
dehydrate @ backend.js:1685
(anonymous function) @ backend.js:917
flush @ backend.js:912
(anonymous function) @ backend.js:900

Versions:
"mobx": "^2.1.5",
"mobx-react": "^3.0.5",

@capaj capaj added the 🐛 bug label May 3, 2016
@AriaFallah
Copy link

AriaFallah commented May 3, 2016

I'm guessing the error is located in this block of code when it tries to report itself as observed.

[
    "concat",
    "every",
    "filter",
    "forEach",
    "indexOf",
    "join",
    "lastIndexOf",
    "map",
    "reduce",
    "reduceRight",
    "slice",
    "some"
].forEach(funcName => {
    const baseFunc = Array.prototype[funcName];
    Object.defineProperty(ObservableArray.prototype, funcName, {
        configurable: false,
        writable: true,
        enumerable: false,
        value: function() {
            this.$mobx.atom.reportObserved();
            return baseFunc.apply(this.$mobx.values, arguments);
        }
    });
});

and that for some reason your array doesn't have this.$mobx defined. I'm not sure if that's the case, but it would mean for some reason this line in the constructor is not executing.

Object.defineProperty(this, "$mobx", {
  enumerable: false,
  configurable: false,
  writable: false
});

How are you creating the observable array causing the error? Perhaps the constructor is not being invoked for a strange reason.

@capaj
Copy link
Member Author

capaj commented May 3, 2016

I have a json object which has an array on one attribute. In the render I run that reduce. It is indeed that 5th line from the bottom which throws.

@AriaFallah
Copy link

AriaFallah commented May 3, 2016

@capaj can you paste the code you use to create the relevant part of that JSON object? Without seeing it the only thing I can guess is that somehow your constructor is being called with the wrong this.

@mweststrate
Copy link
Member

@capaj did you have a debugger open when running into this? I saw this once before, when using some debugging tools and the this was not bound correctly inside the getter / setter function (!) when inspecting values through the development tools.

@mweststrate
Copy link
Member

If you have a small reproduction btw i gladly dive into it

@capaj
Copy link
Member Author

capaj commented May 5, 2016

@mweststrate I will try to isolate a small case this weekend. Please keep open until then

@capaj
Copy link
Member Author

capaj commented May 8, 2016

I tried to reproduce it, but I cannnot anymore. It might be that I was doing something weird like lodash _.clone(observable) and the merging it with other observable using _.merge().
In any case, closing this bug.

@capaj capaj closed this as completed May 8, 2016
@jeffgran
Copy link

I have seen bug reports of this happening in production, but have not been able to reproduce it either.

Maybe the OS/browser has to do with it? This is the user agent info from my bug report:

browserName: Android
browserVersion: 2.3.5
osName: Android 2.3
userAgent: Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; SCH-I500 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

@mweststrate
Copy link
Member

I think it is browser related indeed, when googling for Object.defineProperty and Android 2.3 there are quite some hits, among this one: https://mail.mozilla.org/pipermail/es-discuss/2014-March/036778.html

So there seem to be some workarounds, but not sure how feasible (or needed) that is.

@marbemac
Copy link

For anybody that runs into this - for us it was redux dev tools. They work for the most part, until they didn't :). Not sure what combination of mobx usage led to the issue, but disabling redux fixed it.

@natenorberg
Copy link

Leaving this in case someone runs into the same issue I did.

I hit this error after calling lodash's cloneDeep on an observable object. Replacing it with toJS from mobx fixed it for me

@danielkcz
Copy link
Contributor

Please, refrain from commenting on old and closed issues as only a few people will see that. Open new issue with all necessary details.

@mobxjs mobxjs locked as resolved and limited conversation to collaborators Aug 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants