Typeof msg.payload === 'number' throws exception

#1

Hi,

I tried to convert a msg.payload to return round value of the number. But the function does not return the number.

The js code is here:

module.exports = function(RED) {
   function MathOPeration(config) {
       RED.nodes.createNode(this,config);
       var node = this;
	   this.operation = config.operation;
       this.on('input', function(msg) {
		   if (typeof (msg.payload) === 'number'){
			   if(node.operation === "floor"){
				   //var x = parseInt(node.operation);
				   //var a = Math.floor(x);
				   //msg.payload = msg.payload.a;
				   // msg.payload = Number(msg.payload);
					msg.payload = msg.payload.Math.floor(msg.payload);
			   }
			   else if(node.operation === "lower"){
				   msg.payload = msg.payload.toLowerCase();
				 
			   }
			   else if (node.operation === "round"){
		   msg.payload = msg.payload.Math.round(node.operation);
		   
			   }
			   else if (node.operation === "sqrt"){
					var y = parseInt(node.operation);
				    var c = Math.sqrt(y);
				   msg.payload = msg.payload.c;
		   
			   }
           node.send(msg);
		   }else{
			   node.error("Payload not number",msg);
		   }

While in debug the return shows undefined. How can I get the floor value?

Regards.

Abu Naser Mohammad Muinuddin

#2

There are a few places in your code where you use msg.operation expecting it to contain the number, but it contains the name of the operation.

As this is the assignment from the Node-RED Coursera course, please use the Course forum for questions. We cannot complete the assignment for you.

Nick