0

I don't know why i am getting this error

  • uncaughtException: await is only valid in async function

as i am using async keyword at getChild function still i am getting this error

  • my code:
async function filterFile(folderPath) {
  try {
    wantExt = [".jpg"];
    let parts;
    const paths = await checkFileLoc(folderPath, 3);
    const otherFiles = [];

    for (const filePath of paths) {
      parts = filePath.split("/");
      let splitFileName = parts[parts.length - 1].split(".");
      if (wantExt.includes(`.${splitFileName[splitFileName.length - 1]}`)) {
        otherFiles.push(filePath);
      }
    }
    let ignoreFile = otherFiles.filter((x) =>
      x.endsWith("_bio.jpg")
    );
    let test = otherFiles.filter((x) => !ignoreZipFile.includes(x));
    return { test };
  } catch (error) {
    console.log("error:", error);
  }
}

async function getChild(parents) {
  return new Promise(function (resolve, reject) {
    Shop.findAll({
      where: {
        shop_no: parents.id,
      },
      attributes: ["id", "name"],
    })
      .then((children) => {

        let gotValue= await filterFile(children);
        console.log(gotValue);

        resolve(children);
      })
      .catch(function (err) {
        reject(err);
      });
  });
}

and if i remove async from the function then I am getting gotValue as promise don't know how to get value

5
  • Is checkFileLoc an async function? Commented May 26, 2021 at 10:01
  • yes @freakish my checkFileLoc is also an async function Commented May 26, 2021 at 10:01
  • 2
    Oh, right, look at your getChild function. You use await but inside a non-async anonymous function (which is inside non-async Promise). You can't do that. Commented May 26, 2021 at 10:03
  • @freakish I posted an answer below by changing that line Commented May 26, 2021 at 10:04
  • Shop.findAll is already a Promise. Why wrap it inside another Promise? Commented May 26, 2021 at 10:07

4 Answers 4

1

Try adding async to callback function in findAll

async function getChild(parents) {
  return new Promise(function (resolve, reject) {
    Shop.findAll({
      where: {
        shop_no: parents.id,
      },
      attributes: ["id", "name"],
    })
      .then(async (children) => { // Add it here

        let gotValue= await filterFile(children);
        console.log(gotValue);

        resolve(children);
      })
      .catch(function (err) {
        reject(err);
      });
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Shop.findAll is already a Promise. Why wrap it inside another Promise?
1

You missed async keyword before

(children) => {
     let gotValue = await filterFile(children);
     console.log(gotValue);
     resolve(children);
}

function. It should be

async (children) => {
     let gotValue = await filterFile(children);
     console.log(gotValue);
     resolve(children);
}

Comments

1

"await is only valid in async function" Exactly as the error says, await is used inside an async function.

const fn = async () => { await someAsyncFunction() };

So in your scenario, make the callback async as well to await the filterFile code.

.then(async (children) => {
    let gotValue= await filterFile(children);
    console.log(gotValue);

    resolve(children);
 })

Comments

1
async function filterFile(folderPath) {
  try {
    wantExt = [".jpg"];
    let parts;
    const paths = await checkFileLoc(folderPath, 3);
    const otherFiles = [];

    for (const filePath of paths) {
      parts = filePath.split("/");
      let splitFileName = parts[parts.length - 1].split(".");
      if (wantExt.includes(`.${splitFileName[splitFileName.length - 1]}`)) {
        otherFiles.push(filePath);
      }
    }
    let ignoreFile = otherFiles.filter((x) =>
      x.endsWith("_bio.jpg")
    );
    let test = otherFiles.filter((x) => !ignoreZipFile.includes(x));
    return { test };
  } catch (error) {
    console.log("error:", error);
  }
}

async function getChild(parents) {
  try {
    const children = await Shop.findAll({
      where: {
        shop_no: parents.id,
      },
      attributes: ["id", "name"],
    });
    let gotValue= await filterFile(children);
    console.log(gotValue);
    return children;
  } catch(err) {
    throw(err);
  }
}

2 Comments

Shop.findAll is already a Promise. Why wrap it inside another Promise?
@JeremyThille you're right, I edited the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.