Node.js在函数外部返回值

时间:2018-11-08 21:18:40

标签: node.js json aws-lambda

im试图在follow函数之外获取两个值 lat lng ,但是没有结果,有什么建议吗?我想拥有两个值 latOk lngOk 供将来使用。

var https = require('https');
var lat;
var lng;

function getCall(callback) {
  //initialize options values, the value of the method can be changed to POST to make https post calls

  var options = {
    protocol: 'https:', 
    host: 'maps.googleapis.com',
    path: '/my_path',
    method: 'GET'
  };

  //making the https get call
  https.request(options, function(res) {
    let data = ''

    // A chunk of data has been recieved from request stream.  
    res.on('data', function(chunk) {
        data += chunk;
    });

    // Once the whole response has been received, return response as callback
    res.on('end', () => {

      var resultsjs = JSON.parse(data);
      lat = resultsjs.location.lat;
      lng = resultsjs.location.lng;

    });

  })
  .on('error', function(err) {
    callback(err);
  });

}

var latOk = lat;
var lngOk = lng;

0 个答案:

没有答案