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

#each Object.keys() works in REPL but not local #334

Closed
IBwWG opened this issue Mar 4, 2017 · 5 comments
Closed

#each Object.keys() works in REPL but not local #334

IBwWG opened this issue Mar 4, 2017 · 5 comments

Comments

@IBwWG
Copy link

IBwWG commented Mar 4, 2017

REPL: https://svelte.technology/repl/?version=1.10.0&gist=3e428aac25e50c913430174dd9160b65

Same sort of code locally (with svelte 1.10.0, svelte-cli 1.3.4) generates this output:

	var eachBlock_0_value = root.Object.keys(root.routes)

This doesn't exist, so the console gives an error: Uncaught TypeError: Cannot read property 'keys' of undefined

Additionally, trying to add this workaround:

        helpers: {
        		routePaths() {
        				return Object.keys(routes)
        		}
        },

...and then call #each routePaths() results in babel giving an Unexpected token error. It specifies line 152, which is beyond the end of my source file, so I'm assuming it's looking at some intermediate file when it says that, but there's nothing left in public/dist after it errors out.

@Conduitry
Copy link
Member

I can't reproduce the first issue. When running svelte-cli on the REPL example you gave, I'm getting var eachBlock_value = ( 'Object' in root ? root.Object : Object ).keys(root.rows);, the same as the REPL is generating. Accessing certain whitelisted globals directly in template files should be available since 1.2.5 via #185 / #186. That output you gave is definitely not from the component you have in the REPL (e.g., it uses routes instead of rows.) Can you give a minimal example that's currently failing for you with the CLI tool?

As for your attempted workaround - routes is undefined within that function. Helper functions need to be pure so you should pass the routes in when you call it. Alternately, you could use computed properties.

@IBwWG
Copy link
Author

IBwWG commented Mar 4, 2017

Interesting, neither can I, using svelte compile (which uses 1.9.1, my globally installed svelte at the moment)--same output as you posted. I guess I'd better check into the rollup setup here, but I had made sure to yarn upgrade svelte and yarn upgrade svelte-cli first, so I must still be missing something.

It's true it's not the exact same code, but I could see that it wasn't entirely relevant what the data key was called, hence my phrasing "same sort of code". If I can't figure out the svelte version problem then I might have to post a repo, since my minimal example does not fail, but let's see.

Thanks for pointing out my workaround flaw, very true! Although I should be able to use this.get('routes') instead of passing it around, right? But that change doesn't fix the babel error. Anyway, I guess I should put that aside, because it may be moot, until I figure out the version trouble.

@IBwWG
Copy link
Author

IBwWG commented Mar 4, 2017

Aha, what was needed: yarn upgrade rollup-plugin-svelte. Not a svelte version issue (per se) after all.

@IBwWG IBwWG closed this as completed Mar 4, 2017
@Conduitry
Copy link
Member

Although I should be able to use this.get('routes') instead of passing it around, right?

Actually no. this is not the component instance inside of helper functions, which is by design. Helper functions are meant to be pure, and shouldn't depend on anything but their arguments.

@IBwWG
Copy link
Author

IBwWG commented Mar 4, 2017

Oh, I see. Thanks, that's a helpful insight. I'd forgotten about function purity not just being about side-effects.

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

No branches or pull requests

2 participants