one ppment
one ppment

Reputation: 41

SyntaxError: await is only valid in async function but async is already included

I don't know why I am getting this problem, I already have async included

here is the code

const fetch = require("node-fetch")

module.exports = {
  name: "find",
  aliases: ["f"],
  run: async (message) => {
    message.attachments.forEach((attachment) => {
      const url = attachment.url;
      console.log(url);

      const res1 = await fetch(
        `https://saucenao.com/search.php?db=999&output_type=2&testmode=1&numres=16&url=${url}`
      );
      const data1 = await res1.json();
      console.log(data1);
    });
  },
};

Upvotes: 1

Views: 77

Answers (1)

Jai248
Jai248

Reputation: 1649

Try this

module.exports = {
  name: "find",
  aliases: ["f"],
  run: async (message) => {
    message.attachments.forEach(async (attachment) => {
      const url = attachment.url;
      console.log(url);

      const res1 = await fetch(
        `https://saucenao.com/search.php?db=999&output_type=2&testmode=1&numres=16&url=${url}`
      );
      const data1 = await res1.json();
      console.log(data1);
    });
  },
};

async should be written in a function that has await method. In your code await is written in forEach function, not in the run.

Upvotes: 1

Related Questions