“等待仅在异步函数中有效” discord.js

问题描述 投票:0回答:2

我正在编写此代码,并在下面带有箭头的行上得到“等待仅在异步函数中有效”的信息。我已经进行了一些搜索,但是我不确定是哪里出了问题。

       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'
                    },
javascript node.js discord.js
2个回答
0
投票

您需要使您的函数当前异步。您可以使用以下这些选项之一来做到这一点。 (取决于您的函数类型)

箭头功能:async (parameters) => {}

正常功能:async function(parameters){}

希望我能帮上忙!


0
投票

要使用await,使用await的语句必须直接在async函数的主体中。

例如:

此作品

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
}
© www.soinside.com 2019 - 2024. All rights reserved.