简体   繁体   中英

Node Js formidable Heroku

On my local webserver the following code works perfectly, if i send post request with postman.

    var postData;

    var form = new formidable.IncomingForm();
    form.parse(req,function(err,fields,files){
        postData = fields;
    });

    var userName = postData.userName;
    var password = postData.password;

On Heroku i get the following error:

TypeError: Cannot read property 'userName' of undefined

Local is variable postData a form object. On Heroku it us undefined.

This is because of async nature of node.js. Try something like this -

var userName, password;

form.parse(req,function(err,fields,files){
        postData = fields;
        userName = postData.userName;
        password = postData.password;
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM