[英]“await is only valid in async function” discord.js
我正在編寫此代碼,並且在下面帶有箭頭的行中得到“等待僅在異步函數中有效”。 我已經做了一些搜索,但我不完全確定出了什么問題。
if (message.attachments.first()) {
console.log("cool file.")
if (verifyFile(message.attachments.first())) {
console.log("epic file!!")
---> await request.get(message.attachments.first().url).then(async (data) => {
fs.writeFileSync(`${directory}/${message.author.id}_unobfuscated.lua`, data)
var options = {
'method': 'POST',
'url': 'https://obfuscator.aztupscripts.xyz/Obfuscate',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
您需要使您的 function 當前處於異步狀態。 您可以使用以下這些選項之一來完成。 (取決於您的 function 類型)
箭頭 function: async (parameters) => {}
正常 function: async function(parameters){}
希望我能幫上忙!
要使用await
,使用await
的語句需要直接在async
function 的主體中。
例如:
這有效
const getValue = async () => 5
const myFunction = async () => {
let val = await getValue()
console.log(val)
}
這不
const getValue = async () => 5
const myFunction = () => {
let val = await getValue()
console.log(val)
}
在你的情況下:
let myFunc = async () => {
...othercode
if (message.attachments.first()) {
console.log("cool file.")
if (verifyFile(message.attachments.first())) {
console.log("epic file!!")
---> await request.get(message.attachments.first().url).then(async (data) => {
fs.writeFileSync(`${directory}/${message.author.id}_unobfuscated.lua`, data)
var options = {
'method': 'POST',
'url': 'https://obfuscator.aztupscripts.xyz/Obfuscate',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
...othercode
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.