![](/img/trans.png)
[英]NodeJS + AngularJS + File Upload - connect-busboy doesn't call callback
[英]NodeJS error with connect-busboy, “TypeError: Cannot call method 'on' of undefined”
我有这段代码:
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,这就是为什么不将RE_MIME.test(mime(req)))
添加到请求。
我的示例文件的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,emmable,multiparty等无法解析请求,除非它是multipart/form-data
(如果您正在上传文件)。
确保您在index.js文件中:
var busboy = require('connect-busboy');
// ...
var app = express();
// ...
// and
app.use(busboy()); // I had exact the same error when this line was missing in index.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.