【发布时间】:2021-08-07 05:16:06
【问题描述】:
您好,我正在尝试使用 nodejs 编写一个文件,并给它一个目录以将其保存在 mongodb 数据库中 但我收到错误 “数据”参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例。收到未定义 当我尝试写入文件时
这是我的API
router.post('/create2', (req, res) => {
var tc = new Testcase({
name: req.body.name,
upload: fs.writeFile('./uploads/'+req.body.name+'.robot', req.body.step1 , function (err) {
if (err) throw err; console.log('Results Received');
}),
run : req.body.run,
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,
});
tc.save((err, doc) => {
if (!err) { res.send(doc); }
else { console.log('Error in Employee Save :' + JSON.stringify(err, undefined, 2)); }
});
});
这是我的测试用例架构:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const testcaseSchema = new Schema({
name: {type : String, required : true , unique : true},
upload: String,
run: String,
modify: String,
delete: String,
step1: String,
step2: String,
step3: String,
step4: String,
step5: String,
step6: String,
step7: String,
checked: { type: Boolean, default: false }
});
module.exports = mongoose.model('testcase', testcaseSchema, 'testcases');
【问题讨论】:
-
req.body.step1的值是多少?做一个console.log(req.body.step1)看看它是什么。 -
即使我使用邮递员用字符串填充它,它也会给我一个“0”
-
对不起,它是“未定义”而不是 0
标签: node.js mongodb express fs writefile