0

I am trying to receive in result fields value to operate them a little down in the code But I have undefined in console.log Please explaine why and how I can get my fields down in the code

var multiparty = require('multiparty')

module.exports.login = function(req, res) {
    var form = new multiparty.Form();
 const result = form.parse(req, function(err, fields, files) {
      console.log(err, fields, files);
        return fields
    })
console.log('result=>',result)//undefined

    res.send('Hello from Express!')
}; 

I understand that I need to use prommises but I dont know how, please show me by your code sample

    var multiparty = require('multiparty')

module.exports.login = function(req, res) {
    var form = new multiparty.Form();
 var result  = () => { form.parse(req, function(err, fields, files) {
      console.log(err, fields, files);
      return new Promise(function(resolve, reject) {
       resolve(fields);

    })
})}
console.log('result=>',result.resolve)

    res.send('Hello from Express!')
};
Imediasun
  • 81
  • 1
  • 1
  • 8

2 Answers2

1

Is undefined because function passed in parse method is callback and you can't get return from callback. try this

var multiparty = require('multiparty')
module.exports.login = function(req, res) {
    var form = new multiparty.Form();
    form.parse(req, function(err, fields, files) {
       console.log(err, fields, files);
       console.log('result=>',fields)

       res.send('Hello from Express!')
    })
};
Artur T
  • 334
  • 3
  • 4
  • May be I can put fields variable somewhere to use it after parse method ? As you can see I am trying to use prommises but no results in my case – Imediasun Mar 20 '19 at 20:28
  • in the parse callback function call another outside function that can save given argument from parse. create new function and call it. like save(fields) – Artur T Mar 20 '19 at 20:32
  • Please show by the code I dont understand that – Imediasun Mar 20 '19 at 20:36
  • It would be easier if you take an intro course to javascript. Async is a broad topic and could be hard to explain in this site. – Rash Mar 20 '19 at 20:38
  • Please show how it can be done it is ineresting for me Thanks By Promise in callback – Imediasun Mar 20 '19 at 20:45
  • *"you can't get return from callback"* - This is not entirely true. The value returned by the callback function is passed to function/method that accepted callback. So it could be returned to you via the function that accepted the callback. `[1, 2, 3].map(nr => nr + 1)` is an good example of this. Here the return value of the provided callback is returned to you for each element. This is not the case for `parse` since the method always returns `undefined` (has no return value). Otherwise the answer is fine. – 3limin4t0r Mar 20 '19 at 20:46
0

I'm not familiar with multiparty and don't know if the callback provided is called synchronous or asynchronous. The shortest solution is still the answer of Artur T, simply moving all the code into the callback. However if the code gets more complex this might not be the best solution. You should also be on the lookout for callback hell.

If the callback is called in a synchronous matter you can simply do the following:

var result,
    form = new multiparty.Form();

form.parse(req, function(err, fields, files) {
  console.log(err, fields, files);
  result = fields;
  // ^ set the result variable instead of returning
});

// ...

If the callback is called in an asynchronous matter things get more complicated, and you have to wrap the callback into the promise creation.

var form = new multiparty.Form();

const result = new Promise(resolve => {
  form.parse(req, function(err, fields, files) {
    console.log(err, fields, files);
    resolve(fields);
    //        ^ resolve the promise with fields as passed value
  });
});

result.then(fields => {
  console.log('result=>', fields);
  res.send('Hello from Express!');
});

Alternatively you can change your function into an async function (meaning the return value will be wrapped in a promise). Which is relatively new, but codes cleaner.

module.exports.login = async function(req, res) {
  //                     ^ notice the async keyword

  var form = new multiparty.Form();
  const result = await new Promise(resolve => {
    form.parse(req, function(err, fields, files) {
      console.log(err, fields, files);
      resolve(fields);
    });
  });

  console.log('result=>', result);
  res.send('Hello from Express!');
};

The whole structure can be simplified if you leave out the logging in-between.

const [err, fields, files] = await new Promise(resolve => 
  form.parse(req, (...args) => resolve(args)));
//                     ^                 ^
// collect all arguments into an array and use that as resolve value

// ...

For more explanation about arrow functions, promises and async function have a look at the documentation. This last example also uses destructing assignment and the spread operator.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52