foreach的未定义错误突然发生(数组express node js)



im使用foreach从url获取数组(http://localhost:3000/main?a=aaa.jpg&a=bbb.jpg(。它以前工作,但突然停止工作。一个未定义的错误原因。这是我的代码:

//Define module
var express = require('express');
var app = express();
const { exec } = require('child_process');
//extract function
function extract (req,res,next){
res.write(`filename : ${req.query.a}`);
const filename = req.query.a;
console.log(filename);
filename.forEach((name) => {
console.log(name);
exec(`find wi_file/* -type f \( -name "${name}" \) -print0 | tar -rvf try.tar --null -T -`,(err,stdout,stderr)=>{
if (err){
console.log(`exec error : ${err}`);
return;
};
});
});
next();
};
//main function
function main (req,res,next){
res.write('nkuor dok n');
res.end();
};
app.use(extract);
app.get('/main',main);
app.listen(3000);

有人说这可能是由于失踪造成的。我认不出一个。有人能告诉我是什么原因导致了我的错误吗。这是运行后的错误

您的filename未定义。这就是它抛出错误的原因。filename必须是array,然后可以像上面使用的那样将其与foreach一起使用。如果文件名是类似数组的,您也可以添加检查

if(Array.isArray(filename)){
//do something with filename
}

最新更新