首页 > 解决方案 > SyntaxError: await 仅在异步函数 discord.js 中有效

问题描述

我正在尝试创建一个命令来从 API 获取数据以显示在图像上,但我不断收到此错误:

    /media/bobby/Hardrive/NEW PC/Public/commands/now.js:16
            const background = await Canvas.loadImage('https://lgbtqlounge.xyz/assets/img/canvas.png')
                               ^^^^^
SyntaxError: await is only valid in async function
    at wrapSafe (internal/modules/cjs/loader.js:931:16)
    at Module._compile (internal/modules/cjs/loader.js:979:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:903:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/media/bobby/Hardrive/NEW PC/Public/index.js:14:21)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47

这是我的代码:

const fetch = require("node-fetch")
const Canvas = require("canvas")

module.exports = {
    name: 'now',
    description: 'Tells you what is live on LGBTQLounge Radio!',
    async execute(message, args) {
        fetch("http://api.lgbtqlounge.xyz/radio").then(res => res.json()).then(res => {
            const title = res.song.song
            const artist = res.song.artist
            const art = res.song.art
            const currentDJ = res.currentdj.name

            const canvas = Canvas.createCanvas(1250, 500)
            const ctx = canvas.getContext('2d')
            const background = await Canvas.loadImage('https://lgbtqlounge.xyz/assets/img/canvas.png')
            ctx.drawImage(background, 0, 0, canvas.width, canvas.height)
            ctx.strokeStyle = '#74037b'
            ctx.strokeRect(0,0, canvas.width, canvas.height)
            ctx.font = 'bold 48px "Noto Sans", sans-serif'
            ctx.fillStyle = '#ffffff'
            ctx.fillText(`${title} by ${artist}`, canvas.width / 2.5, canvas.height / 2.6)
            ctx.font = 'bold 72px "Noto Sans", sans-serif'
            ctx.fillStyle = '#ffffff'
            ctx.fillText(currentDJ, canvas.width / 2.5, canvas.height / 1.6)
            ctx.beginPath()
            ctx.arc(235, 244, 150, 0, Math.PI * 2, true)
            ctx.closePath()
            ctx.clip()
            ctx.drawImage(art, 85, 95, 300, 300)
            const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'now.png')
          
            message.channel.send(attachment);
        })
    }
}

如果有人可以帮助我解决这个问题,将不胜感激!

提前感谢您的任何帮助,我希望每个人都度过了愉快的一天(它不会让我发布,因为其中大部分是代码,所以必须详细说明一下,所以我正在享受我一生中最有趣的事情)

标签: javascript

解决方案


正如错误所说,await仅在async函数中有效,您需要将代码包装在async函数中。您的execute函数是,但不是异步async的直接父函数,它是.const background = await Canvas.loadImage.then

fetch("http://api.lgbtqlounge.xyz/radio").then(res => res.json())
     .then(async res => {
            const title = res.song.song
            const artist = res.song.artist
            const art = res.song.art
            const currentDJ = res.currentdj.name

            const canvas = Canvas.createCanvas(1250, 500)
            const ctx = canvas.getContext('2d')
            const background = await Canvas.loadImage('https://lgbtqlounge.xyz/assets/img/canvas.png')
            ctx.drawImage(background, 0, 0, canvas.width, canvas.height)
            ctx.strokeStyle = '#74037b'
            ctx.strokeRect(0,0, canvas.width, canvas.height)
            ctx.font = 'bold 48px "Noto Sans", sans-serif'
            ctx.fillStyle = '#ffffff'
            ctx.fillText(`${title} by ${artist}`, canvas.width / 2.5, canvas.height / 2.6)
            ctx.font = 'bold 72px "Noto Sans", sans-serif'
            ctx.fillStyle = '#ffffff'
            ctx.fillText(currentDJ, canvas.width / 2.5, canvas.height / 1.6)
            ctx.beginPath()
            ctx.arc(235, 244, 150, 0, Math.PI * 2, true)
            ctx.closePath()
            ctx.clip()
            ctx.drawImage(art, 85, 95, 300, 300)
            const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'now.png')
          
            message.channel.send(attachment);
        })

制作回调函数async将解决您的问题。


推荐阅读