Aakash
Aakash

Reputation: 167

async keyword not working on function using node js

I don't know why i am getting this error

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

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

Upvotes: 0

Views: 144

Answers (4)

kayuapi_my
kayuapi_my

Reputation: 508

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

Upvotes: 1

Isaac Khoo
Isaac Khoo

Reputation: 545

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

Upvotes: 1

Majid Karimizadeh
Majid Karimizadeh

Reputation: 98

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

Upvotes: 1

Vinod Bhatia
Vinod Bhatia

Reputation: 111

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

Upvotes: 1

Related Questions