0

The following sequence of calling functions is right but still Token is undefined and res.json({ verification : Token }); returns undefined.

  module.exports = function (server) {
    var { Lib } = require('Lib');
    var lib = new Lib;
    var Token = null;
    var body = '';

    return function verification(req, res, next) {
      res.setHeader('Access-Control-Allow-Origin', '*');
      res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
      res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
      res.setHeader('Access-Control-Allow-Credentials', true);

      req.on('data', function (chunk) {
        body = JSON.parse(chunk);
      });
      req.on('end', function () { 

          let updateUUId = function (data) {
              console.log('updateUUId', data.VSMS);
              return lib.GenerateToken();
          };

        let checkVerification = function (data,callback) {
            console.log('checkVerification', data.UID);
              return callback(data);
          };

          let checkUUID = function (data,callback) {
              console.log('checkUUID', data);
              return callback(data, updateUUId);
          };

        switch (req.method) {
          case 'POST':
                Token = checkUUID(body, checkVerification);
            body = '';
            break;
          default:
            response = json.stringify({ 'error': 'Not A POST' });
            break;
        }
      });
      req.on('error', function (err) {
        res.write(JSON.stringify({ 'error': err.message }));
        res.end();
      });
      console.log(Token);
      res.json({ verification : Token });
    };
  };
Milan Velebit
  • 1,933
  • 2
  • 15
  • 32
RSA
  • 1,417
  • 4
  • 22
  • 37
  • 1
    Yes of course it's undefined, you will only assign to it in that future `end` callback (or even never, in case an error happens instead). Just put the `res.json` call inside that very callback as well! – Bergi Nov 09 '17 at 11:34
  • Great help @Bergi. I appreciated for your help. – RSA Nov 09 '17 at 13:37

0 Answers0