如何在此功能之外发出请求

时间:2016-11-24 16:51:57

标签: javascript node.js express

问题是params是在函数范围内定义的,所以当我尝试在函数外部发出请求时,它会显示未解析的变量或类型参数

我有什么方法可以改写这个。谢谢你的时间

var https = require('https');
var querystring = require('querystring');
function test(access_token)
{
    // setup our request parameters
    var myreq = {
        'location' : '1200, NewYork, USA',
    };

    // stringify our request parameters
    var myreqstr = querystring.stringify(myreq);



    // setup params for the request
    var params = {
        host: 'api.yelp.com',
        port: '443',
        path: '/v3/businesses/search?' + myreqstr, // include the URL parameters
        method: 'get',
        headers : {
            'Authorization' : 'Bearer ' + access_token,
        }
    };

    // call back function will show the results
    callback = function(response) {
        var str = '';
        response.on('data', function (chunk) {
            str += chunk;
        });

        response.on('end', function () {
            console.log(str);
        });
    };

    // make the request
    var req = https.request(params, callback).end();
}

令牌由此回调函数

发送
callback = function(response) {
    var str = '';


    response.on('data', function (chunk) {
        str += chunk;
    });

    response.on('end', function () {
        var tokens = JSON.parse(str);


        test(tokens.access_token);
    });
};

0 个答案:

没有答案