Using global array variables in template

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?

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)

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)

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.

<!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.

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.

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