[英]NODE.js, fs module: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefine
I have this code:我有这段代码:
const Blog = require('../models/blog');
const formidable = require('formidable');
const slugify = require('slugify');
const stripHtml = require('string-strip-html');
const _ = require('lodash');
const { errorHandler } = require('../helpers/dbErrorHandler');
const fs = require('fs');
exports.create = (req, res) => {
let form = new formidable.IncomingForm()
form.keepExtensions = true
form.parse(req, (err, fields, files) => {
if(err) {
return res.status(400).json({
error: 'Image could not be uploaded'
});
};
const { title, body, categories, tags} = fields
let blog = new Blog();
blog.title = title;
blog.body = body;
blog.slug = slugify(title).toLowerCase();
blog.mtitle = `${ title } | ${ process.env.APP_NAME }`;
blog.mdesc = stripHtml(body.substring(0,160));
blog.postedBy = req.user._id;
if(files.photo) {
if(files.photo.size > 10000000) {
return res.status(400).json({
error: 'Image should be less then 1mb in size'
});
}
blog.photo.data = fs.readFile(files.photo.path);
blog.photo.contentType = files.photo.type;
}
blog.save((err, result) => {
if(err){
return res.status(400).json({
error: errorHandler(err)
});
}
res.json(result);
})
});
};
when I try to send the file(any_photo.jpeg) via Postman, I get this error:当我尝试通过 Postman 发送文件 (any_photo.jpeg) 时,出现此错误:
node:internal/fs/utils:676
throw new ERR_INVALID_ARG_TYPE(propName, ['string', 'Buffer', 'URL'], path);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined
at Object.openSync (node:fs:592:10)
at Object.readFileSync (node:fs:468:35)
at /Users/XXX/backend/controllers/blog.js:37:34
at zalgoSafe (/Users/XXX/backend/node_modules/dezalgo/dezalgo.js:20:10)
at f (/Users/XXX/backend/node_modules/once/once.js:25:25)
at IncomingForm.<anonymous> (/Users/XXX/backend/node_modules/formidable/src/Formidable.js:183:9)
at IncomingForm.emit (node:events:513:28)
at IncomingForm._maybeEnd (/Users/XXX/backend/node_modules/formidable/src/Formidable.js:612:10)
at /Users/XXX/backend/node_modules/formidable/src/Formidable.js:390:14
at Array.<anonymous> (/Users/XXX/backend/node_modules/formidable/src/PersistentFile.js:77:7) {
code: 'ERR_INVALID_ARG_TYPE'
}
not sure where exactly the problem, maybe the versions, maybe the path.. I am sending the photo in postman using body -> form-data, how the path is built there - is not clear, not sure if the problem the path.. I have tried to run with function as a second argument, like this:不确定问题到底出在哪里,也许是版本,也许是路径。我正在使用 body -> form-data 在 postman 中发送照片,那里的路径是如何构建的 - 不清楚,不确定问题是否出在路径上。 . 我尝试将 function 作为第二个参数来运行,如下所示:
fs.readFileSync(files.photo.path, function (err, data) {
if (err) throw err;
})
also does not work;也不起作用;
I've tried to use the fs.readFile - also does not work.我试过使用 fs.readFile - 也不起作用。 I've also tried fs.readFileSync(files.photo.filepath) - result is the same error.
我也试过 fs.readFileSync(files.photo.filepath) - 结果是同样的错误。
I solved this problem, it was necessary to do: add callback function, add the filepath name, change the option from readFileSync to readFile我解决了这个问题,有必要这样做:添加回调 function,添加文件路径名称,将选项从 readFileSync 更改为 readFile
blog.photo.data = fs.readFile(files.photo.filepath, function (err, data) {
if (err) throw err;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.