上面是我的代码,但是,我收到一个错误:-
‘Parsing Error: The keyword ‘await’ is reserved’
发生错误的行如下。如果可以的话请帮助我。
我得到了这段代码第一行的错误
如果您能提供帮助,将不胜感激,因为我在这个问题上苦苦挣扎。再次谢谢你!
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
const avatar = await Canvas.loadImage(buffer);
ctx.drawImage(avatar, 25, 25, 200, 200);
if (!channel) return;
const canvas = Canvas.createCanvas(700, 250);
const ctx = canvas.getContext('2d');
const { body:buf } = await snekfetch.get('https://cdn.glitch.com/6ec6dccb-f1a9-4c03-b4f1-d6c639e188c9%2Fwallpaper.jpg?1532779841254');
const background = await Canvas.loadImage(buffer);
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
ctx.strokeStyle = '#74037b';
ctx.strokeRect(0, 0, canvas.width, canvas.height);
// Slightly smaller text placed above the member's display name
ctx.font = '28px sans-serif';
ctx.fillStyle = '#ffffff';
ctx.fillText('Welcome to the server,', canvas.width / 2.5, canvas.height / 3.5);
// Add an exclamation point here and below
ctx.font = applyText(canvas, `${member.displayName}!`);
ctx.fillStyle = '#ffffff';
ctx.fillText(`${member.displayName}!`, canvas.width / 2.5, canvas.height / 1.8);
ctx.beginPath();
ctx.arc(125, 125, 100, 0, Math.PI * 2, true);
ctx.closePath();
ctx.clip();
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
const avatar = await Canvas.loadImage(buffer);
ctx.drawImage(avatar, 25, 25, 200, 200);
const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');
channel.send(`Welcome to the server, ${member}!`, attachment);
});
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
ctx.strokeStyle = '#74037b';
ctx.strokeRect(0, 0, canvas.width, canvas.height);
// Slightly smaller text placed above the member's display name
ctx.font = '28px sans-serif';
ctx.fillStyle = '#ffffff';
ctx.fillText('Welcome to the server,', canvas.width / 2.5, canvas.height / 3.5);
// Add an exclamation point here and below
ctx.font = applyText(canvas, `${member.displayName}!`);
ctx.fillStyle = '#ffffff';
ctx.fillText(`${member.displayName}!`, canvas.width / 2.5, canvas.height / 1.8);
ctx.beginPath();
ctx.arc(125, 125, 100, 0, Math.PI * 2, true);
ctx.closePath();
ctx.clip();
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
const avatar = await Canvas.loadImage(buffer);
ctx.drawImage(avatar, 25, 25, 200, 200);
const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');
channel.send(`Welcome to the server, ${member}!`, attachment);
});
答案 0 :(得分:4)
await
仅在异步函数内有效。从您发布的代码中看不到函数定义,但我的第一个猜测是它是一个普通的旧函数。
例如如果您的代码如下所示:
function main() {
// ...code
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
// ...code
}
然后您会遇到问题,需要在async
关键字之前先使用function
关键字
如果要尝试使用箭头功能,将有类似的要求:
const main = async () => {
// code...
const {body: buffer} = await snekfetch.get(member.user.displayAvatarURL);
// more code...
}
还要注意,async关键字仅适用于它立即附加到的函数,而不适用于该函数内部定义的每个函数。因此,例如,您不能这样做:
const main = async () => {
// code...
const getAvatar = () => {
const {body: buffer} = await snekfetch.get(member.user.displayAvatarURL);
};
// more code...
}
这将是语法错误,因为await在未声明为异步的函数中使用,相反,您需要这样做:
const main = () => {
// code...
const getAvatar = async () => {
const {body: buffer} = await snekfetch.get(member.user.displayAvatarURL);
};
// more code...
}