为什么在尝试使用busboy解析POST请求时会出现未定义的问题?使用MEAN.IO堆栈

时间:2014-07-11 08:51:22

标签: express middleware mean.io

我正在尝试使用busboy-connect来解析我发送给服务器的请求文件。当我打印出我的请求对象时,我得到了这个巨大的Angular.identity对象,当我尝试使用这段代码解析对象时:

app.post("/compositions/fileUpload", function(req, res){
var fstream;

     req.pipe(req.busboy);

     req.busboy.on('file', function (fieldname, file, filename) {

        console.log("Uploading: " + filename); 

        fstream = fs.createWriteStream('/files/' + filename);

        file.pipe(fstream);

        fstream.on('close', function () {

            res.redirect('back');

        });
    });

我在“req.pipe(req.busboy)”上输出错误。我确实需要connect-busboy和fs。我也确保告诉我的应用程序使用busboy,所以他们应该在那里。但是,我继续收到此错误:

TypeError: Cannot call method 'on' of undefined
    at IncomingMessage.Readable.pipe (_stream_readable.js:476:8)
    at Object.handle (/home/cyen/development/composelet/source/composelet/packages/compositions/server/routes/compositions.js:38:10)
    at next_layer (/home/cyen/development/composelet/source/composelet/node_modules/express/lib/router/route.js:103:13)
    at Route.dispatch (/home/cyen/development/composelet/source/composelet/node_modules/express/lib/router/route.js:107:5)
    at /home/cyen/development/composelet/source/composelet/node_modules/express/lib/router/index.js:213:24
    at Function.proto.process_params (/home/cyen/development/composelet/source/composelet/node_modules/express/lib/router/index.js:286:12)
    at next (/home/cyen/development/composelet/source/composelet/node_modules/express/lib/router/index.js:207:19)
    at next (/home/cyen/development/composelet/source/composelet/node_modules/express/lib/router/index.js:182:38)
    at next (/home/cyen/development/composelet/source/composelet/node_modules/express/lib/router/index.js:182:38)
    at next (/home/cyen/development/composelet/source/composelet/node_modules/express/lib/router/index.js:182:38)
POST /compositions/fileUpload 500 136.347 ms - -

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

我不明白为什么。我的here似乎还有另一个类似的问题,但尚未解决。有谁知道这个错误可能是由什么引起的? 谢谢!

1 个答案:

答案 0 :(得分:2)

问题是我在全局范围内的模块上面没有要求(connect-busboy)。