簡體   English   中英

如何解決錯誤“SyntaxError: Unexpected token '?'”

[英]How to solve error "SyntaxError: Unexpected token '?'"

我不確定出了什么問題。 我刪除了我的代碼並下載了它,然后再次上傳它,現在我收到了這個錯誤。

代碼: 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 query = {}
  req.url.slice(req.url.split("").indexOf("?")).slice(1).split("&").forEach((e) => {
    const splited = e.split("=")
    query[splited[0]] = splited[1]
  })

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

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

    res.write(JSON.stringify(msg))
    res.end()
  } else if (query.q == "post") {
    let name = query.name.split("%20").join(" ")
    let content = query.content.split("%20").join(" ")
    client.channels.cache.get(query.code).send(`**${name}**: ${content}`)
    msgs[query.code].push({
      "username": name,
      "content": content,
      "type": "1"
    })
    res.end()
  } else if (url == "/robot" && query.istrue == "true") {
    res.write("Robot!")
    res.end()
  } else {
    let path
    if (!query.code) {
      path = "./code.html"
    } else {
      if (!config.chats.includes(query.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中。

所以要使用?? 運算符,您需要更新 repl.it 中的node

您可以通過關注 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 更改為任何其他數字。
另外,將節點 index.js 更改為您要運行的文件。

您收到此錯誤是因為您使用的是不支持某些包可為空的舊版本節點。

只需更改您的節點版本。

您可以使用“nvm”簡單地更改節點版本。 按照這個 git repo https://github.com/nvm-sh/nvm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM