javascript - NodeJS 与 connect-busboy 发生错误, "TypeError: Cannot call method ' 未定义”

标签 javascript node.js express busboy

我有这段代码:

router.route("/post")
        .get(function(req, res) {
            ...
        })
        .post(authReq, function(req, res) {
            ...
            // Get uploaded file
            var fstream
            req.pipe(req.busboy)
            req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
            ...

            var fileSuffix = mimetype.split("/")[1] // Get suffix, may not always be correct
            var tmpFileName = postCount + "." + fileSuffix
            var tmpFileURL = __dirname + "/tmp/" + tmpFileName
            // Start writing
            fstream = fs.createWriteStream(tmpFileURL)
            file.pipe(fstream)
            fstream.on('close', function() {
                 ...
            })
        })
    })
})

这个使用效果很好。不过,我不太清楚发生了什么,但我今天拿起电脑,发现每次尝试发帖时都会出现此错误:

TypeError: Cannot call method 'on' of undefined
    at IncomingMessage.Readable.pipe (_stream_readable.js:494:8)
    at /Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/apiRoutes.js:139:8
    at Layer.handle [as handle_request] (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/layer.js:82:5)
    at next (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:100:13)
    at authReq (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/apiRoutes.js:17:3)
    at Layer.handle [as handle_request] (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/layer.js:82:5)
    at next (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:100:13)
    at next (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:94:14)
    at Route.dispatch (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:81:3)
    at Layer.handle [as handle_request] (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/layer.js:82:5)

_stream_readable.js:501
    dest.end();
         ^
TypeError: Cannot call method 'end' of undefined
    at IncomingMessage.onend (_stream_readable.js:501:10)
    at IncomingMessage.g (events.js:180:16)
    at IncomingMessage.emit (events.js:92:17)
    at _stream_readable.js:943:16
    at process._tickCallback (node.js:419:13)

在我的 index.js 文件中,我有以下代码可以使用 Busboy:

app.use(busboy())

然后这段代码包含我的路线:

app.use("/api", require("./apiRoutes.js")())

提前致谢!

编辑:我做了更多的调试,我想我已经找到了问题所在。 req.busboy 未定义。所以我开始挖掘源代码。以下是 connect-busboy 模块中的一些代码:

if (req.busboy
|| req.method === 'GET'
|| req.method === 'HEAD'
|| !hasBody(req)
|| !RE_MIME.test(mime(req)))
return next()

当我打印出实际值时,busboy不存在,请求方法是'POST',hasBody(req)结果为true,但RE_MIME.test(mime(req)))结果为false,这就是为什么busboy没有添加到请求中。

我的示例文件的 MIME 类型是 image/gif。以下是正则表达式 connect-busboy 正在测试 image/gif(又名 RE_MIME 变量):

/^(?:multipart\/.+)|(?:application\/x-www-form-urlencoded)$/i;

因此,我得出的结论是我误解了 API。 Busboy 仅适用于 HTML 表单吗?

编辑#2:我只是切换到 Multer而且效果很好。但是,我认为我的问题是我需要将文件放置在请求的多部分中;我不确定为什么它之前可以工作。谢谢!

最佳答案

问题是您的请求不是 multipart/form-data,而是 image/gif。 Busboy、multer、formidable、multiparty 等无法解析请求,除非它是 multipart/form-data(如果您正在上传文件)。

关于javascript - NodeJS 与 connect-busboy 发生错误, "TypeError: Cannot call method ' 未定义”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27162564/

相关文章:

node.js - 当我通过 Express 发送 json 对象时,浏览器不显示选项卡 "raw | parsed"

node.js - 无法让nodeJS Express post显示正文数据

javascript - Node JS HTTP GET 请求正文

javascript - CSS 统一码字体

javascript - 为什么这个 jQuery 代码片段在 IE8 中不能像在 Firefox 或 Chrome 中那样工作(包括现场演示)?

javascript - 使用 jquery 加载 php 变量并使用 javascript

node.js - 我如何从 github 安装 npm 模块并构建它?

javascript - 在模糊或单击时隐藏日历的最简单的解决方案

node.js - WebStorm:NodeJS 进程已完成,退出代码为 0

javascript - 在 Sails.js 中从 PostgreSQL 获取 BYTEA