2

How do I pass the value of var flower from inside form.parse() into response.write() so that I can output it?

Answers to similar questions suggest using promises however if that's the way to do it I would appreciate some help in how to apply a promise in this case

var formidable = require("formidable");
function upload(response, request) {
       var form = new formidable.IncomingForm();
       form.on('fileBegin', function(name, file, flower) {
            file.path = './uploads/' + file.name;
            var flowerImage = file.name ;
       });

      form.parse(request,  function(error, fields, files ) {
      });

      response.writeHead(200, {"Content-Type": "text/html"});
      response.write("image name is " + flowerImage);
      response.end();
  }
jojojohn
  • 725
  • 2
  • 10
  • 19
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – ben Jul 06 '15 at 09:50
  • managed to solve by thinking about it in a different way and rephrasing the question and got some help here http://stackoverflow.com/questions/31246875/how-to-pass-the-node-response-stream-object-into-an-event-handler-function?noredirect=1#comment50492233_31246875 – jojojohn Jul 06 '15 at 15:04

2 Answers2

1

You just need to return the response on the Callback itself

function upload(response, request) {

       var form = new formidable.IncomingForm();

       form.on('fileBegin', function(name, file, flower) {
            file.path = './uploads/' + file.name;
            var flowerImage = file.name ;
            this.writeHead(200, {"Content-Type": "text/html"});
            this.write("image name is " + flowerImage);
            this.end();
       }.bind(response));

  }
Khalid
  • 4,730
  • 5
  • 27
  • 50
  • thanks for your answer, I tried this. For some reason it cannot access the response object from inside the form.on() function, I thought n javascript variables can access values in the parent function but it doesn't seem to be doing it here... – jojojohn Jul 06 '15 at 10:03
  • take a look to the update, I used the `Function.prototype.bind` method to set the response as the context of the callback – Khalid Jul 06 '15 at 10:48
  • thanks.. I got really excited about this answer but I'm still not able to get the variable into the response object although I must be getting closer.. – jojojohn Jul 06 '15 at 11:19
  • I reworded the question and reposted using your answer and it is solved now - thanks for you help .. http://stackoverflow.com/questions/31246875/how-to-pass-the-node-response-stream-object-into-an-event-handler-function?noredirect=1#comment50492233_31246875 – jojojohn Jul 06 '15 at 15:03
0

declare the var flowerImage next not the var form

EDIT: My bad... flowerImage is created using an async call... This has been answered many times.

See How do I return the response from an asynchronous call?

Community
  • 1
  • 1
ben
  • 3,558
  • 1
  • 15
  • 28