如何解决错误“SyntaxError:Unexpectedtoken'?'”

我不确定出了什么问题。我删除了我的代码并下载了它然后再次上传,现在我收到了这个错误。

代码:https : //replit.com/@hi12167pies/webcord#index.js(点击code获取code,output获取输出)

错误:

/home/runner/C8AU9ceLyjc/node_modules/discord.js/src/rest/RESTManager.js:32
    const token = this.client.token ?? this.client.accessToken;
                                     ^

SyntaxError: Unexpected token '?'

我不知道出了什么问题,因为它在 node_modules 文件夹中。

如果您在查看它时遇到问题,这里是代码:

const http = require("http")
const discord = require("discord.js")
const client = new discord.Client()
const config = require("./config.json")
const fs = require("fs")
// const readLine = require("readline")
// const rl = readLine.createInterface({
//   input: process.stdin,
//   output: process.stdout
// })

let msgs = {
  "873195510251532348": [],
  "873195522633105429": []
}


client.on("ready", () => {
  console.log("ready discord")
})

client.on("message", (message) => {
  if (message.author.bot) return
  if (!config.chats.includes(message.channel.id.toString())) return

  msgs[message.channel.id].push({
    "username": message.author.tag,
    "content": message.content,
    "type": "0"
  })
})

http.createServer((req,res) => {
  const url = req.url.split("?")[0]
  let qurrey = {}
  req.url.slice(req.url.split("").indexOf("?")).slice(1).split("&").forEach((e) => {
    const splited = e.split("=")
    qurrey[splited[0]] = splited[1]
  })

  if (qurrey.q == "messages") {
    let msg = []

    let i = 0
    while (msgs[qurrey.code].length > i) {
      const e = msgs[qurrey.code][msgs[qurrey.code].length - (i+1)]
      msg.push(e)
      i++
    }

    res.write(JSON.stringify(msg))
    res.end()
  } else if (qurrey.q == "post") {
    let name = qurrey.name.split("%20").join(" ")
    let content = qurrey.content.split("%20").join(" ")
    client.channels.cache.get(qurrey.code).send(`**${name}**: ${content}`)
    msgs[qurrey.code].push({
      "username": name,
      "content": content,
      "type": "1"
    })
    res.end()
  } else if (url == "/robot" && qurrey.istrue == "true") {
    res.write("Robot!")
    res.end()
  } else {
    let path
    if (!qurrey.code) {
      path = "./code.html"
    } else {
      if (!config.chats.includes(qurrey.code)) {
        path = "./invaildcode.html"
      } else {
        path = "./chat.html"
      }
    }
    fs.readFile(path, (er, da) => {
      if (er) res.write("Could not get index.html")
      res.write(da)
      res.end()
    })
  }


}).listen(80, (err) => {
  if (err) throw err
  console.log("listening webserver")
})

client.login(process.env.TOKEN)

我知道我的代码现在不好,我正在重写它,但我仍然想知道错误是什么。

回答

repl.it使用node v12.22.1但无效的合并运算符( ??) 是相对较新的,并已添加到node v14.

所以要使用??操作符,你需要node在 repl.it 中更新。

您可以通过关注lukenzy 的这个 repl.it 论坛帖子来完成。

创建一个文件,命名为 .replit 在里面,复制粘贴以下代码:

run = """
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh |  bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && ."$NVM_DIR/bash_completion"
nvm install 14
node index.js
"""

这将安装并使用最新的 Node.js v14 (14.17.4)。
如果您想使用其他版本,请将 nvm install 14 更改为任何其他数字。
另外,将 node index.js 更改为您要运行的文件。


以上是如何解决错误“SyntaxError:Unexpectedtoken'?'”的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>