小编Myk*_*l_M的帖子

承诺使用Google Maps Geocoder API

我正在尝试创建一组函数,使用Google Maps Geocoder API将一组地址转换为lat long值.

目前,我已成功将地址转换为lat long值,但函数在返回之前完全执行.我知道这是因为它在之后记录正确的lat long值之前会抛出未定义的错误.

我听说javascripts承诺可以解决这类问题所以我做了一些研究,但它似乎没有帮助解决问题.如果我以错误的方式解决这个问题,我很同意这个承诺.

这是相关的代码

 function getPoints(geocoder,map) {
       let locationData = [];
       let latValue;
       for(let i = 0; i < addressData.length; i++){
            let getLatLong = new Promise(function(resolve,reject){
                 latValue = findLatLang(addressData[i].location, geocoder, map);
                 if(latValue!=undefined){
                      resolve(latValue());
                 } else {
                      reject();
                 }
            });
            getLatLong.then(function(){
                 console.log(latValue);
                 //returns a GMap latLng Object.
                 locationData.push( new google.maps.LatLng(latValue[0],latValue[1]));
            })
       }
       return locationData;
  }

function findLatLang(address, geocoder, mainMap) {
       geocoder.geocode({'address': address}, function(results, status) {
            if (status === 'OK') {
                 console.log(results);
                 return [results[0].geometry.location.lat , results[0].geometry.location.lng]; …
Run Code Online (Sandbox Code Playgroud)

javascript google-maps google-maps-api-3 es6-promise

6
推荐指数
1
解决办法
4080
查看次数

How to use an .ENV file with a website

I've made a few node.js bots and web applications that have used .env variables along with the dotenv package to hold all of the API keys.

However, Im currently working on a website that uses node.js with browserify and this method doesn't seem to work. Whenever I try to output the value to the console or use the value in the code it returns undefined.

我该如何解决这个问题,或者如果 .env 根本无法在网络上工作,我将如何隐藏这些值?

我的 .env 示例

ID = B1CRL2WDIW2553
SECRET = 41445d2b99b33ede3ebce0421900b8e9
Run Code Online (Sandbox Code Playgroud)

我的例子

const dotEnv …
Run Code Online (Sandbox Code Playgroud)

javascript environment-variables api-key node.js browserify

5
推荐指数
1
解决办法
3648
查看次数