async 关键字无法使用节点 js 处理函数
async keyword not working on function using node js
我不知道为什么会出现此错误
- uncaughtException: await 仅在异步函数中有效
因为我在 getChild 函数中使用 async 关键字,我仍然收到此错误
- 我的代码:
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);
});
});
}
如果我从函数中删除 async 那么我会得到 gotValue
因为 promise 不知道如何获取值
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);
}
}
尝试向 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);
});
});
}
您之前错过了 async
个关键字
(children) => {
let gotValue = await filterFile(children);
console.log(gotValue);
resolve(children);
}
函数。应该是
async (children) => {
let gotValue = await filterFile(children);
console.log(gotValue);
resolve(children);
}
“await 仅在异步函数中有效”
正如错误所说,在异步函数中使用了 await。
const fn = async () => { await someAsyncFunction() };
所以在你的场景中,让回调异步以及等待 filterFile 代码。
.then(async (children) => {
let gotValue= await filterFile(children);
console.log(gotValue);
resolve(children);
})
我不知道为什么会出现此错误
- uncaughtException: await 仅在异步函数中有效
因为我在 getChild 函数中使用 async 关键字,我仍然收到此错误
- 我的代码:
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);
});
});
}
如果我从函数中删除 async 那么我会得到 gotValue
因为 promise 不知道如何获取值
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);
}
}
尝试向 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);
});
});
}
您之前错过了 async
个关键字
(children) => {
let gotValue = await filterFile(children);
console.log(gotValue);
resolve(children);
}
函数。应该是
async (children) => {
let gotValue = await filterFile(children);
console.log(gotValue);
resolve(children);
}
“await 仅在异步函数中有效” 正如错误所说,在异步函数中使用了 await。
const fn = async () => { await someAsyncFunction() };
所以在你的场景中,让回调异步以及等待 filterFile 代码。
.then(async (children) => {
let gotValue= await filterFile(children);
console.log(gotValue);
resolve(children);
})