formidable: TypeError: Cannot read property 'headers' of undefined

Hi all, Im getting the above error when is referencing the following function.

var formidable = require("formidable");

function upload(response, request){
    console.log("Request handler 'upload' was called");

    var form = new formidable.IncomingForm();

    form.parse(request, function(err, fields, files) {
        fs.renameSync(files.upload.path, "./tmp/test.png");
        response.writeHead(200, {"Content-Type": "text/html"});
        response.write("received image:<br/>");
        response.write("<img src=’/show’ />");
        response.end();
    });
}

Error from Terminal

/Users/info/node_modules/formidable/lib/incoming_form.js:84
  this.writeHeaders(req.headers);
                       ^
TypeError: Cannot read property 'headers' of undefined
    at IncomingForm.parse (/Users/info/node_modules/formidable/lib/incoming_form.js:84:24)
    at Object.upload [as /upload] (/Users/info/Sites/node/requestHandlers.js:30:7)
    at route (/Users/info/Sites/node/router.js:5:19)
    at Server.onRequest (/Users/info/Sites/node/server.js:10:3)
    at Server.EventEmitter.emit (events.js:91:17)
    at HTTPParser.parser.onIncoming (http.js:1785:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)
    at Socket.socket.ondata (http.js:1682:22)
    at TCP.onread (net.js:404:27)

Can anyone advise what the issue may be?

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 18

Most upvoted comments

I’ll take a guess and say it’s because your parameters for your upload function are swapped?