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

toJS() behaves unexpected #1557

Closed
stnwk opened this issue May 24, 2018 · 14 comments
Closed

toJS() behaves unexpected #1557

stnwk opened this issue May 24, 2018 · 14 comments

Comments

@stnwk
Copy link

stnwk commented May 24, 2018

Hi :)

I came across an unexpected behavior from toJS().

It states in the docs:

toJS():
Recursively converts an (observable) object to a javascript structure.

Example 1 (works as expected)

const someIds = observable.array([10, 20, 30]);
const someJSIds = toJS(someIds)

-> someJSIds is a plain js array

But

Example 2 (works unexpected)

const someIds = observable.array([10, 20, 30]);
const options = toJS({ someIds }); // notice i'm passing an object here

-> options.someJSIds is now still an observable array

Taken from the documentation I reckon this is a bug / undesired behavior: it should take (observable) data structure and return a plain js object. Just because the top-level object is not an observable structure, doesn't mean toJS() should not convert possible nested values.

Using mobx 4.3.0

@mweststrate
Copy link
Member

mweststrate commented May 24, 2018 via email

@stnwk
Copy link
Author

stnwk commented May 24, 2018

Ok, thanks for your quick response!

Either way is fine.
But in this case the documentation is misleading as it clearly states "Recursively converts an (observable) object to a javascript structure." - since "(observable)" is writting in parenthesis it gives the impression it would also recursively convert a non-observable object to a javascript structure in my second example.

Do you see what I mean?

@mweststrate
Copy link
Member

mweststrate commented May 24, 2018 via email

@capaj
Copy link
Member

capaj commented May 28, 2018

@stnwk I've ran into the same problem as you a year or so back. We should really patch that documentation there.

@wangyiz4262
Copy link
Contributor

@stnwk @capaj I submitted PR to address the mentioned problem
Refactor toJS and Refactor toJS for mobx4

@y4ure
Copy link

y4ure commented Oct 21, 2018

@wangyiz4262 Thank you for you PR.
I've noticed though with the recurseEverything option it will throw when value is null (actually works for undefined). Is that the expected behaviour?

@wangyiz4262
Copy link
Contributor

Hi @y4ure, thanks for noticing any potential bug! But I didn’t quite understand what you meant. What I can find is a null check in the beginning of the toJS function and that is from master branch which I didn’t change. Would you please elaborate a bit? Thanks a lot!

@y4ure
Copy link

y4ure commented Oct 21, 2018

Given that JSONs can contain null value, I'm expecting toJS to handle null values. However I'm getting this error below instead.

Uncaught TypeError: Cannot convert undefined or null to object
    at Function.getPrototypeOf (<anonymous>)
    at toJSHelper (D:\_Workspace\…bx\lib\mobx.js:2509)
    at toJSHelper (D:\_Workspace\…bx\lib\mobx.js:2528)
    at D:\_Workspace\…bx\lib\mobx.js:2503
    at Array.map (<anonymous>)
    at toJSHelper (D:\_Workspace\…bx\lib\mobx.js:2503)
    at toJS$$1 (D:\_Workspace\…bx\lib\mobx.js:2545)

@mweststrate
Copy link
Member

@y4ure since the original issue is addressed, could you open a new issue for your problem, including a small reproduction in a codesandbox or as PR on the repository? Thanks!

@wangyiz4262
Copy link
Contributor

wangyiz4262 commented Oct 22, 2018

@y4ure @mweststrate Two new PRs have been created to fix this issue, see #1783 for mobx5 and #1784 for mobx4.

@mweststrate
Copy link
Member

Released as 4.5.2 / 5.5.2

@mxs42
Copy link

mxs42 commented May 13, 2019

This behavior is still not covered in documentation.

@mweststrate
Copy link
Member

mweststrate commented May 13, 2019 via email

@lock
Copy link

lock bot commented Jul 21, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants