Search code examples
javascriptarraysangularjsnode.jsnode-red

How to collect strings, sent from angularJS, in NodeRED array?


I'm trying to collect strings I send from AngularJS to a NodeRED array. The AngularJS code looks like this

 this.user =
   {
     medName1: '',
     medTime1: ''
   },
   {
     medName2: '',
     medTime2: ''
   },
   {
    medName3: '',
    medTime3: ''
   };

I'm collecting form data in medName1, medTime1,.. and so on. I'm trying to send this data, over websocket, one by one to NodeRED using the following code

 this.register = function() {
 $scope.sock.send(this.user.medName1);
 $scope.sock.send(this.user.medTime1);
 $scope.sock.send(this.user.medName2);
 $scope.sock.send(this.user.medTime2);
 $scope.sock.send(this.user.medName3);
 $scope.sock.send(this.user.medTime3);
}

register() is called when I click on "submit" button.

My question is - How do I store these strings in a nodeRED array?. Because the way I'm sending it, the string always gets stored in array index 0, overwriting the previous string. I also tried

 $scope.sock.send(JSON.stringify(this.user));

but it sends the entire thing as a string to nodeRED which then makes it impossible to extract the values assigned to medName1, medTime1, and so on.

Can anyone please suggest a way!.. I'll really appreciate your help.


Solution

  • If you send the json.stingify version, you can then use a JSON node in your Node-RED flow to convert it back to the JavaScript object you want.