语法错误 - 意外标识符(带节点的 XMPP)
Posted
技术标签:
【中文标题】语法错误 - 意外标识符(带节点的 XMPP)【英文标题】:Syntax Error - unexpected identifier (XMPP with Node) 【发布时间】:2017-05-21 18:20:50 【问题描述】:语法错误 - 带有节点的意外标识符 XMPP
我是 Node 和 XMPP 的新手,使用 ltx 阅读聊天节时出现错误,错误是:。
const logEntry = 'Received message from ' $from ' with ^ SyntaxError: Unexpected identifier
at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) at Module._compile (module.js:542:28) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10) at run (bootstrap_node.js:390:7) at startup (bootstrap_node.js:150:9)
文件名为:index.js
代码如下:
const Client = require('node-xmpp-client')
, ltx = Client.ltx
const options =
jid: 'bot@localhost',
password: 'tellnoone'
const client = new Client(options)
client.on('online', (connectionDetails) =>
console.log('We are connected!')
console.log(connectionDetails)
sendPresence()
)
const sendPresence = () =>
var stanza = new ltx.Element('presence')
console.log('Sending presence: ' + stanza.toString())
client.send(stanza)
client.on('stanza', (stanza) =>
if (false === stanza.is('message')) return /* Not a <message/> stanza */
const messageContent = stanza.getChildText('body')
if (!messageContent) return /* Not a chat message */
const from = stanza.attr('from')
const logEntry = 'Received message from ' $from ' with
content:\n$messageContent'
console.log(logEntry)
)
【问题讨论】:
【参考方案1】:换行试试
const logEntry = 'Received message from ' $from ' with
content:\n$messageContent'
到
const logEntry = `Received message from $from with content:\n $messageContent`
模板字符串使用反引号而不是引号。
【讨论】:
以上是关于语法错误 - 意外标识符(带节点的 XMPP)的主要内容,如果未能解决你的问题,请参考以下文章
未捕获的语法错误:在 JS 文件中导入 JavaScript 时出现意外标识符