1

I was building a simple chat app with phoenix and elixir. One of the steps was to import Socket and Presence into the Javascript but I'm pretty sure that step is not working for reasons I have no idea about. I put this line at the top of my app.js:

import {Socket, Presence} from "phoenix"

When I debugged this line with debugger, I typed Presence in the console and it gave me a VM1416:1 Uncaught ReferenceError: Presence is not defined. Is there some setup that I could have missed? maybe with brunch or something?

Gazler
  • 83,029
  • 18
  • 279
  • 245
Bitwise
  • 8,021
  • 22
  • 70
  • 161
  • You could try to do `console.log(Presence)` right after the import. I'm not sure if this is _easily debuggagble_ like what you mentioned here due to how the assets are compiled. – Paweł Dawczak Jun 22 '17 at 20:44
  • give a try with `import * as Phoenix from "phoenix"` and use `Phoenix.Presence` – Ankanna Jun 22 '17 at 21:13

1 Answers1

1

The compiled code will be wrapped in an anonymous function, so the scope of Presence will not leak outside of the anonymous function.

If you want Presence to be available globally, you can do something like:

import {Socket, Presence} from "phoenix"
window.Presence = Presence;

Then it will work in your console.

You can read more about JavaScript scope in this answer https://stackoverflow.com/a/500459/219743

Gazler
  • 83,029
  • 18
  • 279
  • 245