0

Above is my code, However, I am getting an Error:-

‘Parsing Error: The keyword ‘await’ is reserved’

The line where my Error is occurring is below. Help me if you can please.

I get the error of the first line of this bit of code

if you can help, it'd be greatly appreciated as I am struggling a lot with this issue. Thank you again!

    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);
});
Manish Balodia
  • 1,863
  • 2
  • 23
  • 37
Scott Elsy
  • 99
  • 2
  • 2
  • 10

1 Answers1

4

await is only valid inside an async function. I can't see the function definition from the code you have posted but my first guess would be that it is a plain old function.

eg. if your code looks like:

function main() {
    // ...code
    const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
    // ...code
}

then you'll have problems, you need the async keyword before the function keyword

If you were to be trying to use arrow functions, a similar requirement would be in place:

const main = async () => {
  // code...
  const {body: buffer} = await snekfetch.get(member.user.displayAvatarURL);
  // more code...
}

Also a note that the async keyword applies only to the function it is immediately attached to, not recursively down to every function that you define inside that function. So for instance, you could NOT do:

const main = async () => {
  // code...
  const getAvatar = () => {
    const {body: buffer} = await snekfetch.get(member.user.displayAvatarURL);
  };
  // more code...
}

That would be a syntax error because await is used inside a function which was not declared as async, instead you would need to do:

const main = () => {
  // code...
  const getAvatar = async () => {
    const {body: buffer} = await snekfetch.get(member.user.displayAvatarURL);
  };
  // more code...
}
Chris O'Kelly
  • 1,863
  • 2
  • 18
  • 35
  • I am getting the messag even if the function is declared async like this `const IndexPage = async () => { let input const upload = () => { const files = input.files const hash = await sha1(files[0]) }` – pungggi Oct 13 '18 at 18:46
  • You probably want to ask a separate question or check out https://stackoverflow.com/questions/42964102/syntax-for-async-arrow-function or similar, but there are a number of things that may be going wrong for you, node.js not updated to a version that supports async/await (or whatever other environment it runs in not having that support) being the first that comes to mind – Chris O'Kelly Oct 16 '18 at 01:50
  • Oh actually its just that code... this is one reason that asking your own question works out better, the syntax highlighting for code really doesn't make this easy in comments. – Chris O'Kelly Oct 16 '18 at 01:52
  • I've tried to update the answer to cover the situation you have as well, but if you have further issues you oughta ask a new question. as an aside unrelated to async/await, you should terminate your declarations with semi-colons... `let x const y = z` is a syntax error and should be `let x; const y = z;` Also I guess there is more to the code than what's shown byt as it is it appears that `hash` and `upload` would be immediately out of scope... neither of those arrow functions appears to return anything – Chris O'Kelly Oct 16 '18 at 02:05
  • Hi @Chris-okelly yes the problem was the misplaced async keyword in the outher function.. – pungggi Oct 16 '18 at 18:49