【问题标题】:How do I fix ‘Parsing Error: The keyword ‘await’ is reserved’如何修复“解析错误:保留关键字‘await’”
【发布时间】:2019-01-06 08:00:12
【问题描述】:

上面是我的代码,但是,我得到一个错误:-

‘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);
});

【问题讨论】:

  • 你使用的是什么版本的节点?

标签: node.js discord.js


【解决方案1】:

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...
}

【讨论】:

  • 即使函数像这样声明为异步const IndexPage = async () => { let input const upload = () => { const files = input.files const hash = await sha1(files[0]) },我也会收到消息
  • 您可能想问一个单独的问题或查看stackoverflow.com/questions/42964102/… 或类似的问题,但您可能会遇到很多问题,node.js 未更新到支持的版本async/await(或它在没有该支持的任何其他环境中运行)是第一个想到的
  • 哦,实际上它只是那个代码......这是问你自己的问题效果更好的原因之一,代码的语法突出显示在 cmets 中确实不容易做到这一点。
  • 我已尝试更新答案以涵盖您的情况,但如果您还有其他问题,您应该提出一个新问题。作为与异步/等待无关的旁白,你应该用分号终止你的声明......let x const y = z 是一个语法错误,应该是let x; const y = z; 另外我猜代码比显示的内容更多看来hashupload 将立即超出范围......这些箭头函数似乎都没有返回任何内容
  • 嗨@Chris-okelly 是的,问题是在外部函数中放错了异步关键字..
猜你喜欢
  • 2021-11-18
  • 2019-03-28
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
相关资源
最近更新 更多