Deepak Reddy
Deepak Reddy

Reputation: 26

Display using template-node in node red(IBM BLUEMIX)

I was trying to get a run-time value(msg.payload) from a function node(node-red) and supply it to a template node(node-red) to display appropriate image according to the input.I used the below code,but the image is not changing according to the choice of input. Below is the code. Please take a look and provide me some insights or what needs to be changed to make it work.

<html> 
  <head> 
     <script type="text/javascript"> 
        function displayImage() { 
          var j=parseInt({{payload}});
          document.getElementById("img").src = images[j]; 
        }

        function startTimer() { 
          setInterval(displayImage,3000); 
        } 

        var images = []; 
        images[0] = "image1.jpg"; 
        images[1] = "image2.jpg";
        images[2] = "image3.jpg";
    </script>
  </head> 
  <body onload="startTimer()">
     <img id="img" src="image1.jpg"/>
  </body> 
</html>

Upvotes: 0

Views: 6302

Answers (2)

stwissel
stwissel

Reputation: 20384

You are dealing with NodeRED. The easiest way to learn about your payload parameter is to use a Debug node (the dark green one) and attach it to the same output as the template node. You then will very quickly see what it is made of. Most likely your payload is a JSON object. Something like this:

 { "name" : "Peter",
   "color" : "Blue",
   "image" : "2"
  }

... or similar.

Then you just change that content in {{ }} to reflect the variable inside the JSON object that has the number you are interested in. Like:

     {{payload.image}}

Double check your function node. If it doesn't end with a return statement (typically return msg) you simply won't have any input.

Upvotes: 0

PaolaDV
PaolaDV

Reputation: 86

Use the console.log(message)and check:

  1. the {{payload}} parameter should be an integer as you expected
  2. the value of J parameter, after its assignment, should an integer between 0-2

Upvotes: 1

Related Questions