简体   繁体   English

SyntaxError: await 仅在异步函数和模块的顶层主体中有效。 不和谐,js

[英]SyntaxError: await is only valid in async functions and the top level bodies of modules. Discord,js

Error:错误:

SyntaxError: await is only valid in async functions and the top level bodies of modules SyntaxError: await 仅在异步函数和模块的顶层主体中有效

[Photo error] https://i.stack.imgur.com/a0hWM.png [照片错误] https://i.stack.imgur.com/a0hWM.png

const client = require('../index.js');

client.on('ready', () => {
  let servers = await client.guilds.cache.size
  let servercount = await client.guilds.cache.reduce((a,b) => a+b.memberCount, 0)

  const activites = [
    `?help | ${servers} servers`,
    `Invite me now! | Watching ${servercount} members`
  ]

  setInterval(()=>{
    const status = activities[Math.floor(Math.random()*activities.length)]
    client.user.setPresence({ activities : [{name : `${status}`}]})
  }, 5000)

  
  console.log(`✅ ${client.user.tag} is ready`)
})

You can use await with only async functions.您只能将 await 与异步函数一起使用。 Checkout the below code where I have changed your client.on callback function to async.查看以下代码,我已将您的 client.on 回调函数更改为异步。

 const client = require('../index.js'); client.on('ready', async() => { let servers = await client.guilds.cache.size let servercount = await client.guilds.cache.reduce((a,b) => a+b.memberCount, 0) const activites = [ `?help | ${servers} servers`, `Invite me now! | Watching ${servercount} members` ] setInterval(()=>{ const status = activities[Math.floor(Math.random()*activities.length)] client.user.setPresence({ activities : [{name : `${status}`}]}) }, 5000) console.log(`✅ ${client.user.tag} is ready`) })

暂无
暂无

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

相关问题 SyntaxError: await 仅在异步函数和模块的顶层主体中有效 - SyntaxError: await is only valid in async functions and the top level bodies of modules ASYNC / AWAIT SyntaxError: await 仅在异步函数和模块的顶层主体中有效 - ASYNC / AWAIT SyntaxError: await is only valid in async functions and the top level bodies of modules javascript await function in script 返回 Uncaught SyntaxError: await is only valid in async functions and the top level body of modules - javascript await function in script returns Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules await 仅在异步函数和模块的顶级主体中有效 - await is only valid in async functions and the top level bodies of modules await 仅在异步函数和循环中模块的顶层主体中有效 - await is only valid in async functions and the top level bodies of modules in loop 使用 async await 在 chrome 扩展中出现此错误“await 仅在异步函数和模块的顶层主体中有效” - Getting this error "await is only valid in async functions and the top level bodies of modules" in chrome extension with async await 全局调用异步 function 时出错:“等待仅在异步函数和模块的顶层主体中有效”? - Error in global call to async function: "await is only valid in async functions and the top level bodies of modules"? 打开 AI 示例代码不起作用“等待仅在异步函数和模块的顶级主体中有效” - Open AI example code not working "Await is only valid in async functions and the top level bodies of modules" await 仅在异步函数和模块的顶层主体中有效 javascript 表示错误 - await is only valid in async functions and the top level bodies of modules javascript express error 语法错误:await 仅在异步函数 discord.js 中有效 - SyntaxError: await is only valid in async function discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM