[英]SyntaxError: “await” is only valid in an “async” function - Help Needed
So, I am building a Bot (Specifically for Discord) and I am very new to JavaScript, and coding in general.所以,我正在构建一个机器人(专门用于 Discord),我对 JavaScript 和一般编码非常陌生。
I was watching a tutorial about a purging messages from an area command (You tell the bot how many messages to delete, and it does so.)我正在观看有关从区域命令中清除消息的教程(您告诉机器人要删除多少条消息,它会这样做。)
Everything was going swimmingly, I had worked for almost an hour on this command, and after I saved and ran the "bot" in my CMD, I got this error (Shown in the error section, and expected results area)一切都很顺利,我在这个命令上工作了将近一个小时,在我保存并运行 CMD 中的“机器人”后,我收到了这个错误(显示在错误部分和预期结果区域)
Basically:基本上:
const fetched = await message.channel.fetchMessages({limit: args[0]});
const fetched = await message.channel.fetchMessages({limit: args[0]});
SyntaxError: await is only valid in async function
SyntaxError: await 仅在异步中有效 function
But I am pretty sure it is an async
function, I am still very new to coding so I may have closed it off or something, not sure.但我很确定它是一个
async
function,我对编码还是很陌生,所以我可能已经关闭它或其他东西,不确定。
FYI in line 31 I thought I "turned on" an async
thing, but maybe I closed it.仅供参考,在第 31 行我以为我“打开”了一个
async
的东西,但也许我关闭了它。
I didn't send the complete code.我没有发送完整的代码。
As I am very new to coding, I couldn't really do much.因为我对编码很陌生,所以我真的做不了太多。 I did add an ";"
我确实添加了一个“;” (without the quotes) which solved one error, but not the one I am asking about.
(不带引号)解决了一个错误,但不是我要问的那个。
// Purge messages command.
if (msg.startsWith(prefix + 'PURGE')) { // COmmand checks like "!PING", but ``startWith`` because you'll be adding a number.
// Wrapping in an async because ``awaits`` only work in asyncs.
async function purge() {
message.delete(); // Deletes command trigger, to clean up the chat more fully.
//Now, we want to check if the user has the `Moderator` role.
if (!message.member.roles.find("name", "Moderator")) { // This checks to see if they DONT have that role (the "!" inverts the true/false)
message.channel.send('You need the Moderator role to use this command!');
return; // This returns the code, so the rest doesn't run.
}
}
// Check if the argument is a number.
if (isNaN(args[0])) {
// Posts that the message is NaN (not a number)
message.channel.send('Your argument was not a number. \n Usage: ' +
prefix + 'purge <amount>'); //\n turns into a new line.
// Stops the actions, so it doesn't start deleting messages.
return;
}
const fetched = await message.channel.fetchMessages({limit: args[0]}); // This takes the argument number.
Expected Result: Bot turns on and deletes a bulk number of specified messages.预期结果:机器人打开并删除大量指定消息。
Complete Error Message:完整的错误信息:
C:\Users\xxxx\OneDrive\Desktop\Xxxxx xxx\xxx.js:49 const fetched = await message.channel.fetchMessages({limit: args[0]});
C:\Users\xxxx\OneDrive\Desktop\Xxxxx xxx\xxx.js:49 const fetched = await message.channel.fetchMessages({limit: args[0]}); // This takes the argument number.
// 这需要参数编号。
SyntaxError: await is only valid in async function at Module._compile (internal/modules/cjs/loader.js:723:23) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
SyntaxError: await 仅在 Module._compile (internal/modules/cjs/loader.js:723:23) 的异步 function 中有效 Object.Module._extensions.js:79/ 10) 在 Module.load (internal/modules/cjs/loader.js:653:32) 在 tryModuleLoad (internal/modules/cjs/loader.js:593:12) 在 Function.Module._load (internal/modules/cjs /loader.js:585:3) 在 Function.Module.runMain (internal/modules/cjs/loader.js:831:12) 在启动时 (internal/bootstrap/node.js:283:19) 在 bootstrapNodeJSCore (internal/引导程序/node.js:622:3)
You have to mark your method as async
.您必须将您的方法标记为
async
。
Examples from Mozilla's async function . Mozilla 的异步 function 示例。
async function asyncCall() {
console.log('calling');
var result = await resolveAfter2Seconds();
console.log(result);
// expected output: 'resolved'
}
var parallel = async function() {
console.log('==PARALLEL with await Promise.all==');
// Start 2 "jobs" in parallel and wait for both of them to complete
await Promise.all([
(async()=>console.log(await resolveAfter2Seconds()))(),
(async()=>console.log(await resolveAfter1Second()))()
]);
}
Your last fetch needs to be wrapped in an async function in order to use await
.您的最后一次提取需要包装在异步 function 中才能使用
await
。 Either than or you need to utilize then
to resolve the promise.或者您需要使用
then
来解析 promise。
const getData = async (arg) => {
const data = await message.channel.fetchMessages({limit: args})
return await data.json()
}
With that example you'd have to resolve getData
, which means you'd either have to use then
or wrap its context in async
-- so wrap it in an async function.对于该示例,您必须解析
getData
,这意味着您必须使用then
或将其上下文包装在async
中 - 因此将其包装在异步 function 中。
Or:或者:
message.channel.fetchMessages({limit: args[0]})
.then(response => response.json())
.then(data => {
/* do something here */
})
I couldn't understand all about your code.我无法理解您的代码。 but I think you should add "async" top of that function.
但我认为你应该在 function 的顶部添加“异步”。
async function func() {
await ~
}
modify your code like that style像这种风格一样修改你的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.