When use worker, I get Uncaught TypeError: Cannot read property 'defaultView' of undefined #163

Closed
yokeholy opened this Issue Jan 21, 2015 · 27 comments

Projects

None yet

4 participants

@yokeholy

I'm using 4.1.0 minified version. Everything works fine until I turn on worker.
No matter which way I call Papa Parse (Papa.parse(file,config) or $(file).parse(config)), I will get this error in my console and then nothing happens (the parser stops).

@mholt
Owner
mholt commented Jan 21, 2015

What kind of environment are you running Papa in? I've seen that reported before; it's probably the same as #114 but we weren't able to pin it down.

I have a hunch now, though. Papa Parse explicitly forbids the use of the byte order mark (\ufeff) as a delimiter, so this character is defined as a variable for convenience (in the minified version, it's e.Papa.BYTE_ORDER_MARK). I just noticed that the minified version seems to replace the sequence \ufeff with the literal byte order mark. Maybe that's problematic.

Could you take a look at position 12,708 in papaparse.min.js - there's the unescaped unicode character there. @tony-cocco, you also had this issue, if you could take a look too I would appreciate it. Try removing that line and the reference to it in the BAD_DELIMITERS array.

If you take out the byte order mark, does it work?

@mholt mholt added the under review label Jan 21, 2015
@tony-cocco

I can't say definitively, but when I stopped passing the papaparse library through minification my issue went away. I would suggest @yokeholy try using the un-minified library and see if that solves his problem.

If possible, might be worthwhile to know what the file that's being parsed looks like.

@yokeholy

I tried both minified and un-minified versions. The problem exists whenever worker is on.
I tested with large files (90MB+) as well as a single line string. Quoted strings with comma delimiter.
My environment is Angular+jQuery+BootstrapJS.

@yokeholy

One thing through my research pops up: http://stackoverflow.com/questions/3936211/jquery-cannot-read-property-defaultview-of-undefined-error

It seems like the scope has changed. I wonder if it's anything to do with Angular because I'm calling the parser within an Angular controller scope.

@tony-cocco

I was also using Angular as well. But, the scope changing could still be the cause. My issue came from the grunt task messing with the papa library though.

Apparently, I was using version 3.1.3 of papa. Sorry I can't be of more help.

@yokeholy

So I did another test and put the parser caller function outside of my Angular scope, unfortunately, the problem still exists.

@yokeholy

@mholt I looked into position 12708, I didn't find the unescaped unicode character there.
e.Papa.BAD_DELIMITERS=["\r","\n",'"',e.Papa.BYTE_ORDER_MARK];e.Papa.WORKERS_SUPPORTED=!!e.Worker;
is what I have around that position.. I have 4.1.0

@tony-cocco

I think his suggestion was to remove e.Papa.BYTE_ORDER_MARK from that array. Something you can try till he responds.

@yokeholy

Did not do the trick.. :(

@mholt
Owner
mholt commented Jan 21, 2015

That, and also remove the definition of e.Papa.BYTE_ORDER_MARK which should remove that character entirely.

I need your help since I can't seem to reproduce the problem.

@yokeholy

@mholt I removed both, still errors out.

@mholt
Owner
mholt commented Jan 21, 2015

Whoops, we both commented at the same time.

Are you sure that the byte order mark character is completely removed? The only other thing I can think of, based on a search for "defaultView of undefined" brings up some jQuery-related stuff on Stack Overflow. Maybe try removing the jQuery stuff (these lines: https://github.com/mholt/PapaParse/blob/master/papaparse.js#L37-L129)

@mholt
Owner
mholt commented Jan 21, 2015

Oh, and I just saw your reply above about what you found on Stack Overflow. I'm not familiar with Angular, so I can't explain why things are breaking with the minified version and using workers. Maybe it has something to do with Angular not playing well in a worker environment or something.

I would recommend using the un-minified version then, as @tony-cocco suggested. That's still so weird...

What if you call Papa from outside the Angular controller scope?

@yokeholy

Yes I removed them both..
And trying removing the entire jQuery stuff still doesn't help.

@yokeholy

@mholt I did call it outside the Angular controller scope, tried un-minified version. Seems like I'm in bad luck haha

@mholt
Owner
mholt commented Jan 21, 2015

Thanks for trying all that!

You're still getting it with the un-minified code too? That's just bizarre. I can guarantee that error isn't coming from Papa Parse, since defaultView is not defined/found/used anywhere in its code.

What if you try it without Angular? I know it'll probably break your app (just try it), but I'm betting that something there is trying to run code that expects a DOM and there is no DOM in a worker.

@yokeholy

Yea that might take a long time :P
BTW, Throughout my js files, I found the word defaultView only in jQuery.

@mholt
Owner
mholt commented Jan 21, 2015

Interesting. Is the minified Papa Parse in the same file as any other library, particularly any that use (or are) jQuery? (This is not uncommon for some build processes, to just combine all the minified libraries into the same file.)

@yokeholy

Based on this SO question: http://stackoverflow.com/questions/5453319/undefined-type-error-defaultview-with-basic-jquery

I'm just throwing a wild guess here: maybe the worker is manipulating something that is not applicable to the window object.

@yokeholy

@mholt Yes. All these libraries are combined in one file.

@tony-cocco

@mholt reminded me that when I removed papa from the build chain it was not longer being concatted with others. Try isolating that include.

@mholt
Owner
mholt commented Jan 21, 2015

BINGO! That's it. See this FAQ: http://papaparse.com/faq#combine

This is because a worker thread has to be given an entire JS file - not just a function - to execute. Workers have their own environment and don't even share memory. There's no DOM, and I guarantee that at least one of those other libraries, if not all of them, are not expecting to be executed in a worker thread, devoid of a DOM.

@mholt mholt closed this Jan 21, 2015
@mholt mholt added discussion and removed under review labels Jan 21, 2015
@yokeholy

Ahh I feel like an idiot now coz I read that entire FAQ before posting the issue here... hehe

@yokeholy

Hmm I guess that's the cause of #114 too. Right @tony-cocco ?

@tony-cocco

@yokeholy most definitely!

@bluej100
Collaborator

Some glorious day there will be cross-browser support for spinning up workers from code blobs instead of URLs, and then we can kill this confusion.

@mholt
Owner
mholt commented Jan 21, 2015

Ha, yeah. In the meantime, I'll clarify the FAQ and bump that question higher. Good work, team.

@mholt mholt added a commit that referenced this issue Jan 21, 2015
@mholt Explaining solution to #163 more clearly 53a11b2
@nathancahill nathancahill added a commit to nathancahill/PapaParse that referenced this issue May 22, 2015
@nathancahill nathancahill More visible explanation of #163 in the docs 4602d05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment