![](/img/trans.png)
[英]Javascript: SyntaxError: await is only valid in async function
[英]SyntaxError: “await” is only valid in an “async” function - Help Needed
所以,我正在構建一個機器人(專門用於 Discord),我對 JavaScript 和一般編碼非常陌生。
我正在觀看有關從區域命令中清除消息的教程(您告訴機器人要刪除多少條消息,它會這樣做。)
一切都很順利,我在這個命令上工作了將近一個小時,在我保存並運行 CMD 中的“機器人”后,我收到了這個錯誤(顯示在錯誤部分和預期結果區域)
基本上:
const fetched = await message.channel.fetchMessages({limit: args[0]});
SyntaxError: await 僅在異步中有效 function
但我很確定它是一個async
function,我對編碼還是很陌生,所以我可能已經關閉它或其他東西,不確定。
僅供參考,在第 31 行我以為我“打開”了一個async
的東西,但也許我關閉了它。
我沒有發送完整的代碼。
因為我對編碼很陌生,所以我真的做不了太多。 我確實添加了一個“;” (不帶引號)解決了一個錯誤,但不是我要問的那個。
// 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.
預期結果:機器人打開並刪除大量指定消息。
完整的錯誤信息:
C:\Users\xxxx\OneDrive\Desktop\Xxxxx xxx\xxx.js:49 const fetched = await message.channel.fetchMessages({limit: args[0]}); // 這需要參數編號。
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)
您必須將您的方法標記為async
。
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()))()
]);
}
您的最后一次提取需要包裝在異步 function 中才能使用await
。 或者您需要使用then
來解析 promise。
const getData = async (arg) => {
const data = await message.channel.fetchMessages({limit: args})
return await data.json()
}
對於該示例,您必須解析getData
,這意味着您必須使用then
或將其上下文包裝在async
中 - 因此將其包裝在異步 function 中。
或者:
message.channel.fetchMessages({limit: args[0]})
.then(response => response.json())
.then(data => {
/* do something here */
})
我無法理解您的代碼。 但我認為你應該在 function 的頂部添加“異步”。
async function func() {
await ~
}
像這種風格一樣修改你的代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.