0

I'm reading the Phoenix Guides 1.4.6 and I'm at the Chapter Presence. So I follow all the steps of this chapter and my console javascript complains for this:

app.js:1 Uncaught Error: Module build failed: SyntaxError: /Users/romenigld/workspace/phoenix/hello/assets/js/app.js: Identifier 'socket' has already been declared (21:4)

  19 | import {Socket, Presence} from "phoenix"
  20 | 
> 21 | let socket = new Socket("/socket", {
     |     ^
  22 |   params: {user_id: window.location.search.split("=")[1]}
  23 | })
  24 | 
    at Parser.raise (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:6322)
    at ScopeHandler.checkRedeclarationInScope (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:3754)
    at ScopeHandler.declareName (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:3720)
    at Parser.checkLVal (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:8006)
    at Parser.parseVarId (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:10441)
    at Parser.parseVar (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:10412)
    at Parser.parseVarStatement (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:10234)
    at Parser.parseStatementContent (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:9830)
    at Parser.parseStatement (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:9763)
    at Parser.parseBlockOrModuleBlockBody (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:10340)
    at Parser.parseBlockBody (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:10327)
    at Parser.parseTopLevel (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:9692)
    at Parser.parse (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:11209)
    at parse (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:11245)
    at parser (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/core/lib/transformation/normalize-file.js:170)
    at normalizeFile (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/core/lib/transformation/normalize-file.js:138)
    at runSync (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/core/lib/transformation/index.js:44)
    at runAsync (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/core/lib/transformation/index.js:35)
    at process.nextTick (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/core/lib/transform.js:34)
    at process._tickCallback (:4000/internal/process/next_tick.js:61)
    at eval (app.js:1)
    at Object../js/app.js (app.js:81)
    at __webpack_require__ (app.js:20)
    at eval (app.js:1)
    at Object.0 (app.js:92)
    at __webpack_require__ (app.js:20)
    at app.js:69
    at app.js:72
(anonymous) @ app.js:1
./js/app.js @ app.js:81
__webpack_require__ @ app.js:20
(anonymous) @ app.js:1
0 @ app.js:92
__webpack_require__ @ app.js:20
(anonymous) @ app.js:69
(anonymous) @ app.js:72

my code is in this repo.

And if I delete the let it complains a new error:

ReferenceError: Can't find variable: socket
WebSocket connection to 'ws://localhost:4000/socket/websocket?token=undefined&vsn=2.0.0' failed: Unexpected response code: 500
rld
  • 2,603
  • 2
  • 25
  • 39
  • *Phoenix Guides 1.4.6 and I'm at the Chapter Presence* -- Google reveals no such thing. Don't you think it would make it easier for people to help you if you actually included a link to the guides since presumably you know where they are? – 7stud May 25 '19 at 19:32
  • I will Update, thank's again!! – rld May 26 '19 at 10:48

1 Answers1

5

Your error is:

Identifier 'socket' has already been declared

which means you can't write:

let socket = "abc"
let socket = "def"

And, you are in fact doing that in app.js:

//********* HERE **************
import socket from "./socket"   

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

//********* AND HERE ***********
let socket = new Socket("/socket", { 
  params: {user_id: window.location.search.split("=")[1]}
})

The line:

import socket from "./socket"   

creates a variable named socket, so you can't subsequently tell js to create another variable named socket, which you do here:

let socket = new Socket("/socket", { 
  params: {user_id: window.location.search.split("=")[1]}
})

I would try getting rid of the following import statement in app.js:

import socket from "./socket" 

If you actually need the imported socket in app.js (which does not appear to be the case), you can give socket a different name when you import it. See here. Alternatively, you can name the socket variable which you declared in app.js something else:

let presence_socket = new Socket("/socket", { 
  params: {user_id: window.location.search.split("=")[1]}
})
7stud
  • 46,922
  • 14
  • 101
  • 127
  • Thank you @7stud for reply! But I try this switch like you post here and complains others error... I think if I follow the guides of Phoenix it will be ok for learn and not expect these things... – rld May 26 '19 at 10:46
  • @rld, Okay, then post your revised code at the bottom of your original question, and also post the new error. *I think if I follow the guides of Phoenix it will be ok for learn and not expect these things*-- I agree. Whomever wrote that guide did sloppy work. – 7stud May 27 '19 at 00:53
  • @rld, I just went through the tutorial, and I got the Presence stuff to work. Because the tutorial recreates one of the files, `room_channel.ex`, the Presence stuff does not build on top of the Channels stuff in the previous tutorial. If you create a new app, e.g. `mix phx.new MyHello` then everything will work as stated in the Presence tutorial. For instance the line `import socket from "./socket"` in `app.js` will be commented out in a new app, and then if you add what the tutorial says to add to app.js, you won't get the `socket` variable conflict. – 7stud May 27 '19 at 03:30
  • @rld, It would actually be easiest if you created a new directory, cd'ed into the new directory, and then used the same name for you app: `hello`. Otherwise, you will make mistakes with module names, etc., when typing out the code in the tutorial. – 7stud May 27 '19 at 03:38
  • thank you for reply again! I will put my computer on guarantee today for change the screen, because of the stangate problem. And when I take back I will try better this solution like you told here. – rld May 27 '19 at 08:04