【发布时间】:2021-07-27 14:44:34
【问题描述】:
您好,我有一个 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 并向表中添加新行时,我希望将返回的值直接存储到变量 run 中 请问我该怎么做,我是nodejs和mongodb的新手
【问题讨论】:
-
请同时添加您的 insertOne / insertMany 方法调用
-
还提供
Testcase类的实现 -
我没有使用 insertOne / insertMany 方法,我发布的是我如何插入到我的数据库中,我添加了我的模块和另一个回复中的实现,请你检查一下
标签: node.js mongodb express fs readfile