当前状态章当前状态Phoenix 1.4.6指南针对标识符“ socket”的加注已经声明

时间:2019-05-25 15:47:51

标签: elixir phoenix-framework elixir-framework phoenix-channels

我正在阅读Phoenix Guides 1.4.6,现在在Chapter Presence。 因此,我遵循本章的所有步骤,而我的控制台javascript对此有所抱怨:

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

我的代码在此repo中。

如果我删除了let,它会抱怨一个新错误:

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

1 个答案:

答案 0 :(得分:4)

您的错误是:

Identifier 'socket' has already been declared

这意味着您不能写:

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

而且,您实际上是在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]}
})

该行:

import socket from "./socket"   

创建一个名为socket的变量,因此您随后无法告诉js创建另一个名为socket的变量,您可以在此处执行此操作:

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

我会尝试摆脱app.js中的以下import语句:

import socket from "./socket" 

如果您确实需要在app.js中导入socket(似乎不是这种情况),则可以在导入socket时使用其他名称。参见here。另外,您可以将在app.js中声明的套接字变量命名为其他名称:

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