如何在Nodejs中使这些函数成为异步函数。

时间:2017-11-05 03:24:42

标签: node.js loopbackjs loopback

在以下代码中,我尝试从应用中获取数据并验证是否存在uuid/UIDverification sms/VSMS生成唯一令牌并返回verification api的正确结果。

但似乎if statements的回报不正确。

我不知道为什么。但是在没有返回令牌之后,我试图将它存储在全局变量中,但它也没有成功:

module.exports = function (server) {
    const http = require('https');
    var request = require('request');
    var { Lib } = require('Lib');
    var lib = new Lib;
    var response ;
    var Token =null;

    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);
        var body = '';
        req.on('data', function (chunk) {
            body += chunk;
        });
        req.on('end', function () {
            switch (req.method) {
                case 'POST':
                    handlePost(JSON.parse(body));
                    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();
        });

        res.json({verifivation: this.Token});
    };

    function handlePost(body) {

        if (checkUUID(body) == 200) {

            updateUUId(body);

        }
    };

    function checkUUID(ctx) {
        var URL = 'https://site.cc/api/uuids?filter[where][uuid]=' + ctx.UID;
        const https = require('https');
        https.get(URL, (res) => {
            res.on('data', (d) => {
                d = JSON.parse(d);
                if (d.length == 1 && d[0].uuid == ctx.UID && d[0].verified == false && d[0].token == "null") {
                    return checkVerification(ctx);;
                }
            });
        }).on('error', (e) => {
            console.error('ERROR:', e);
        });
    };

    function checkVerification(ctx) {

        var URL = 'https://site.cc/api/verifications?filter[where][UID]=' + ctx.UID;
        const https = require('https');
        https.get(URL, (res) => {
            res.on('data', (d) => {
                d = JSON.parse(d);
                if (d.length == 1 && d[0].UID == ctx.UID && d[0].VSMS == ctx.VSMS) {
                    return 200;
                }
            });

        }).on('error', (e) => {
            console.error('ERROR:', e);
        });
    };

    function updateUUId(body) {
        this.Token = lib.GenerateToken();
        var URL = 'https://site.cc/api/uuids/update?where={"uuid"' + ':"' + body.UID + '"}';
        console.log(URL);
        request.post(URL, {
            json: {
                verified: true,
                token: this.Token
            }
        },
        );
    };
};

更新

I tried something like this but fails and I have some problem with callback functions understanding.:

handelpost(response, checkUUID(body)) => {
            var URL = 'http://localhost:3000/api/uuids?filter[where][uuid]=' + body.UID;
            const http = require('http');
            http.get(URL, (res) => {
              res.on('data', (d) => {
                d = JSON.parse(d);
                if (d.length == 1 && d[0].uuid == body.UID && d[0].verified == false && d[0].token == "null") {
                  console.log('48');
                  return checkVerification(body);
                }
              });
            }).on('error', (e) => {
              console.error('ERROR:', e);
            });
          });
          if (response == 200) {
            updateUUId(body);
          }

0 个答案:

没有答案