代码返回错误的值?

时间:2017-11-09 11:10:01

标签: javascript node.js loopbackjs loopback

以下调用函数序列是正确的,但Token仍未定义,res.json({ verification : Token });返回 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 });
    };
  };

0 个答案:

没有答案