您好,我有一个 mongodb 表并使用 multer 文件上传我使用 readFile 来上传文件并在行中搜索某些单词这是我的POST API
router.post('/create',multer({ storage : storage}).any(), (req, res) => {
var sample = fs.readFileSync('./uploads/'+req.files[0].filename,'utf8');
function fileToString() {
let arr = sample.split(/\r?\n/);
arr.forEach((step , idx)=> {
if(step.includes("step")){
console.log(step);
return step;
}
});
}
var tc = new Testcase({
name: req.body.name,
upload: req.files[0].filename ,
run: fileToString(), //this variable is the one i want to store the result of the function in
modify: req.body.modify,
delete: req.body.delete,
step1: req.body.step1,
step2: req.body.step2,
step3: req.body.step3,
step4: req.body.step4,
step5: req.body.step5,
step6: req.body.step6,
step7: req.body.step7,
});
console.log(req.files[0].filename);
tc.save((err, doc) => {
if (err) { res.status(401).send("error") }
else {
res.status(200).send(doc)
}
});
});
函数 fileToString()是有效的,它控制台返回我需要的但是我希望在我运行我的创建 API 并向表中添加新行时,将返回的值直接存储到变量运行中,请问我该怎么做我是 nodejs 和 mongodb 的新手