简体   繁体   English

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

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

I'm not sure what's wrong.我不确定出了什么问题。 I deleted my code and downloaded it then uploaded it again and now I get this error.我删除了我的代码并下载了它,然后再次上传它,现在我收到了这个错误。

Code: https://replit.com/@hi12167pies/webcord#index.js (Click code for code and output for output)代码: https ://replit.com/@hi12167pies/webcord#index.js(点击code为code,output为输出)

Error:错误:

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

SyntaxError: Unexpected token '?'

I have no idea whats wrong since it's in the node_modules folder.我不知道出了什么问题,因为它在 node_modules 文件夹中。

If you have problems viewing it here is the code:如果您在查看它时遇到问题,这里是代码:

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)

I am aware my code is not good right now, I am rewriting it but I still want to know what the error is.我知道我的代码现在不好,我正在重写它,但我仍然想知道错误是什么。

repl.it uses node v12.22.1 but the nullish coalescing operator ( ?? ), is relatively new and was added in node v14 . repl.it使用node v12.22.1 ,但无效合并运算符( ?? ) 相对较新,并已添加到node v14中。

So to use the ??所以要使用?? operator you need to update node in repl.it.运算符,您需要更新 repl.it 中的node

Which you can do by following this repl.it forum post by lukenzy .您可以通过关注 lukenzy 的这个repl.it 论坛帖子来做到这一点。

Create a file and name it .replit Inside it, copy and paste the following code:创建一个文件并命名为 .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 """

This will install and use the latest Node.js v14 (14.17.4).这将安装和使用最新的 Node.js v14 (14.17.4)。
If u want to use a different version, change nvm install 14 to any other number.如果您想使用不同的版本,请将 nvm install 14 更改为任何其他数字。
Also, change node index.js to the file u want to run.另外,将节点 index.js 更改为您要运行的文件。

You are getting this error because you are using an older version of node that didn't support nullable for some packages.您收到此错误是因为您使用的是不支持某些包可为空的旧版本节点。

Simply change node version of yours.只需更改您的节点版本。

You can simply change node versions using 'nvm'.您可以使用“nvm”简单地更改节点版本。 follow this git repo https://github.com/nvm-sh/nvm按照这个 git repo https://github.com/nvm-sh/nvm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM