简体   繁体   English

NODE.js,fs 模块:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型或 Buffer 的实例或 URL。收到未定义

[英]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.

相关问题 [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型或 Buffer 或 URL 的实例。 收到 null - [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received null fs.FileRead -&gt; TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串、缓冲区或 URL 类型之一。 接收类型未定义 - fs.FileRead -> TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type undefined TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串、缓冲区或 URL 类型之一。 收到类型 objectsails.js? - TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be one of type string, Buffer, or URL. Received type object sails.js? Node.js:[ERR_INVALID_ARG_TYPE]:“数据”参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例。 收到未定义 - Node.js: [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined TypeError [ERR_INVALID_ARG_TYPE]:“块”参数必须是字符串类型或 Buffer 或 Uint8Array node.js 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array node.js TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型 Node.JS - TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string Node.JS TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到 Object 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string. Received an instance of Object 无法评估事务:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型或缓冲区或 URL 的实例 - Failed to evaluate transaction: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到未定义和代码:'ERR_INVALID_ARG_TYPE' - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined and code: 'ERR_INVALID_ARG_TYPE' TypeError [ERR_INVALID_ARG_TYPE]: 第一个参数必须是字符串类型或者是 Buffer 或 Uint8Array 的实例。 收到未定义 - TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array. Received undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM