# Using global array variables in template

**URL:** <https://discourse.nodered.org/t/using-global-array-variables-in-template/30461>\
**Category:** General\
**Created:** [21 July 2020 11:00 UTC](https://discourse.nodered.org/t/using-global-array-variables-in-template/30461 "2020-07-21T11:00:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![panepoide](https://avatars.discourse-cdn.com/v4/letter/p/db5fbb/32.png) [@panepoide](https://discourse.nodered.org/u/panepoide)\
**Post date:** [21 July 2020 11:00 UTC](https://discourse.nodered.org/t/using-global-array-variables-in-template/30461/1 "2020-07-21T11:00:35Z")

</div>

Hi, I have a project where I store some sensor values in an array in order to print in the dashboard. These values come from one single sensor, so the array is some kind of history.

Then, in another flow, I would like to pick this array in an HTML template in order to print them in a Google Maps map. I have successfully printed the map with some predetermined points on it but the problem is that I can't find the way to pick the array values. Whenever I use `global.get("exmp")` or `{{global.exmp}}` the map in the dashboard is not longer showed, as if the code stopped in these lines of code.

Is there a way to get a global array variable in a template?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [21 July 2020 11:26 UTC](https://discourse.nodered.org/t/using-global-array-variables-in-template/30461/2 "2020-07-21T11:26:20Z")

</div>

can you share a bit more about how the array looks - and then how you have configured the template / map ?

(As the author of the node-red-contrib-web-worldmap node I'm going to be biased and point you at that as a (probably) easier way ... but)

---

<div class="post-metadata">

**Author:** ![panepoide](https://avatars.discourse-cdn.com/v4/letter/p/db5fbb/32.png) [@panepoide](https://discourse.nodered.org/u/panepoide)\
**Post date:** [22 July 2020 09:23 UTC](https://discourse.nodered.org/t/using-global-array-variables-in-template/30461/3 "2020-07-22T09:23:23Z")

</div>

![Flow Distance](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/3/a3891d11800fdafae81f5c1d99947085ff2035de.png) ![Flow Maps](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a21ec455308fefda28738b2944c6ef6e1b2970e5.png)

Here you can see the flows for the distance and map. In the first one, the 3 global arrays are initialised as well as the max size (12) in `Variable Globale` like this:  
(the `check_0` variable is used to initialise the global variables once in the entirety of the project, I don't know how to do that otherwise)

```auto
global.set("check_0",{"cpt":0});

if(global.get("check_0")===0){
    global.set("latitude1",0);
    global.set("longitude1",0);
    global.set("latitude2",0);
    global.set("longitude2",0);
    global.set("latitude3",0);
    global.set("longitude3",0);
    global.set("taille_max",12);
    global.set("tab_historique1", []);
    global.set("tab_historique2", []);
    global.set("tab_historique3", []);
}

global.set("check_0",1);
return msg;

```

The data (latitude and longitude 1, 2 and 3) are stored in the 3 arrays so that the most recent values are stocked in the end of the arrays. That is done in the `Set_Coordonnées` nodes, but I don't think it's important to show it.

In the Map flow, we have successfully showed a map with a path of points. The problem is when I try to get the `tab_historique1` array. Curious fact, I don't have a problem when I try to get the latitude or longitude variables.

```auto
<!DOCTYPE html>
<html>
<head>
  <title>Supply Chain</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
  <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAxuxffizWf5rlO5UHZE4Rt4POasJY0om4"></script>
  <script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>

  <style type="text/css" media="screen">
    #map {
      position:absolute;
      top: 0; bottom: 0; left: 0; right: 0;
    }
  </style>
</head>
<body>
  <div id="map"></div>
  <script type="text/javascript">

$(document).ready(function(){
  var MAP_ZOOM = 14;//14
  var MARKER_SIZE = 24;
   
  // set center of map
  var map = new google.maps.Map(document.getElementById('map'), {
      zoom: MAP_ZOOM,
      center: {lat: 43.6239695, lng: 7.0509146}, //{lat: 43.737450, lng: 7.048596}
      //mapTypeId: 'satellite'
      mapTypeId: 'terrain'
    });

    var hist11 = {{global.latitude1}}; // does not get error
    var hist11 = {{global.tab_historique1}}; // does get error
    // set path of coordinates
    var flightPlanCoordinates = [
          {lat: 43.6234, lng: 7.0523},
          {lat: 43.6235, lng: 7.0524},
          {lat: 43.6236, lng: 7.0523},
          {lat: 43.6238, lng: 7.0525}
        ];
    var flightPath11 = new google.maps.Polyline({
      path: flightPlanCoordinates,
      geodesic: true,
      strokeColor: '#FF0000',
      strokeOpacity: 1.0,
      strokeWeight: 2
    });

    flightPath11.setMap(map);
});
  </script>
</body>
</html>

```

So if you comment the global array line, you can see a map in the dashboard, otherwise it's just a blanc square.

I have to say that the Map flow was given to us by our professor and the `Map` node is the only node there that I have modified.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [22 July 2020 11:13 UTC](https://discourse.nodered.org/t/using-global-array-variables-in-template/30461/4 "2020-07-22T11:13:31Z")

</div>

in your `function` node "Variable Globale" add the following  
` node.warn(global.get("check_0"))`  
after the first line  
`global.set("check_0",{"cpt":0});`  
and then add the following  
`node.debug("I am in the if statement")`  
right after the  
`if(global.get("check_0")===0){`  
and explain what is happening.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 September 2020 11:13 UTC](https://discourse.nodered.org/t/using-global-array-variables-in-template/30461/5 "2020-09-20T11:13:49Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
