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

Init without args gives 'TypeError': Cannot read property 'node' of undefined #11

Closed
fpapado opened this issue Sep 6, 2018 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@fpapado
Copy link

fpapado commented Sep 6, 2018

The problem

I have an application where I call init with no arguments. If I leave it like that, then the hmr script throws.

If I add {} as the argument, it works.

// This throws
const app = Elm.Main.init();

// This works
const app = Elm.Main.init({});

The source

I traced that back to

var domNode = args['node'] ? wrapDomNode(args['node']) : document.body;

 if (originalInit) {
                module.init = function (args) {
                    var elm;
                    var portSubscribes = {};
                    var portSends = {};
                    var domNode = args['node'] ? wrapDomNode(args['node']) : document.body;

The code there assumes that args already exists, and then checks whether there's a node or not.

A solution

I thought to open an issue instead of sending a drive-by PR.
One solution could be just more ternaries, or splitting it out to something named and nicer to debug.

var domNode = !args ? document.body : args['node'] ? wrapDomNode(args['node']) : document.body;

But the root question I suppose is whether "init without arguments means document.body".
I looked through the docs, and that seems to be the case, at least for the current Browser programs.
It seems that package.elm-lang.org also uses init without args, and possibly more user code.

view-source:https://package.elm-lang.org/packages/elm/browser/latest/Browser#application

@klazuka klazuka added the bug Something isn't working label Sep 6, 2018
@klazuka klazuka self-assigned this Sep 6, 2018
@klazuka klazuka closed this as completed in 882f438 Sep 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants