1

I am unable to get return value from nodejs function to post request. I need "response" from generateHash and want to store in "hashVal" in the above post request

app.post('/filehash', function(req, res) {
    var filePath = req.body.filePath;
    if (!filePath) {
        res.json(getErrorMessage('\'filePath\''));
        return;
    }
    var hashVal = fileHash.generateHash(filePath); 
        logger.info('hashVal---->>>> '+hashVal);
    res.send(hashVal);
});


var generateHash = function(filePath) {
 var algo = 'md5';
  var shasum = crypto.createHash(algo);
  var response = '';
  var s = fs.ReadStream(filePath);
  s.on('data', function(d) { shasum.update(d); });
  s.on('end', function() {
    var d = shasum.digest('hex');
    logger.debug('\n====== Creating filehash \'' + d + '\' ======\n');
    console.log(d);
    response=d;
    return response;
  });
};

4 Answers 4

1

generateHash is an async function. You can't return result like this. You have to return it in a callback

app.post('/filehash', function(req, res) {
    var filePath = req.body.filePath;
    if (!filePath) {
        res.json(getErrorMessage('\'filePath\''));
        return;
    }
    fileHash.generateHash(filePath, function(hashVal){ 
        logger.info('hashVal---->>>> '+hashVal);
        res.send(hashVal);
    });

   });


var generateHash = function(filePath, cb) {
   var algo = 'md5';
   var shasum = crypto.createHash(algo);
   var response = '';
   var s = fs.ReadStream(filePath);
   s.on('data', function(d) { shasum.update(d); });
   s.on('end', function() {
    var d = shasum.digest('hex');
    logger.debug('\n====== Creating filehash \'' + d + '\' ======\n');
    console.log(d);
    response=d;
    return cb(response);
  });
};
Sign up to request clarification or add additional context in comments.

Comments

0

You can't return such way in Node.js due to async nature of it. One thing you can do here use callback

app.post('/filehash', function(req, res) {
    var filePath = req.body.filePath;
    if (!filePath) {
        res.json(getErrorMessage('\'filePath\''));
        return;
    }
    fileHash.generateHash(filePath,function(err,hashVal){
        logger.info('hashVal---->>>> '+hashVal);
        res.send(hashVal);
    }); 
});


var generateHash = function(filePath,callback) {
 var algo = 'md5';
  var shasum = crypto.createHash(algo);
  var response = '';
  var s = fs.ReadStream(filePath);
  s.on('data', function(d) { shasum.update(d); });
  s.on('end', function() {
    var d = shasum.digest('hex');
    logger.debug('\n====== Creating filehash \'' + d + '\' ======\n');
    console.log(d);
    response=d;
    callback(null,response);
  });
};

Comments

0

Looking at your code, you defined your function in a variable named generateHash not as an attribute inside filehash object.

Try replacing:

var hashVal = fileHash.generateHash(filePath);

with

var hashVal = generateHash(filePath);

Comments

0

Add callback to generateHash function and call it in on end event:

var generateHash = function(filePath, cb) {
  ...
  s.on('error', function(err) {
    cb(err);
  });
  s.on('end', function() {
    ...
    cb(null, response);
  });
});

Controller:

app.post('/filehash', function(req, res) {
  ...
  fileHash.generateHash(filePath, (err, hashVal) => {
    if (err) {
      return res.status(500).send(err);
    }
    logger.info('hashVal---->>>> '+ hashVal);
    res.send(hashVal);
  }); 
});

Another, modern way is to use promises:

var generateHash = function(filePath, cb) {
  ...
  return new Promise((resolve, reject) => {
    s.on('error', function(err) {
      reject(err);
    });
    s.on('end', function() {
      ...
      resolve(response);
    });
  });
});

Controller:

app.post('/filehash', function(req, res) {
  ...
  fileHash
   .generateHash(filePath)
   .then(hashVal => {
      logger.info('hashVal---->>>> '+hashVal);
      res.send(hashVal);
    })
    .catch(err => res.status(500).send(err); 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.