How to extract the rpcresponse data


#1

Hi all,

I am very new to exosite and learning nodejs. I have exported the exosite one platform in my nodejs project.
Below is the example of the rpc call:

setInterval(function() {

rpc.call(cik, 'read', [{alias: "message"}, {}],
		  function(err, rpcresponse, httpresponse) {
		  if (err) {
		    console.log('error: ' + err);  
		  } else {
		    if (rpcresponse[0].status === 'ok') {
		      console.log(JSON.stringify(rpcresponse[0].result));
		      console.log(JSON.parse(JSON.stringify(rpcresponse[0].result)));			      
		    } else {
		      console.log('Bad status: ' + rpcresponse[0].status);
		    }
		  }
		});}, 1000);

And the output result is:

[[1456369867,“hellow me”]]
[ [ 1456369867, ‘hellow me’ ] ]

My question is, what is the easiest way to extract just the result object exclusive the timestamp? I am expecting the result should be just a string hellow me without [ ] and no timestamp.

Thanks!

Handi


#2

Hi Handi,
Glad to hear you are getting started with Exosite!
In your code, you are logging out the “result” parameter of the first (0th) object in the rpcresponse array returned in response to your call:

rpcresponse[0].result

As you can see in your console.log, this is an array of arrays with only one array in it. The array inside is in the format Exosite uses for a point of data in a dataport, which is an array of length 2 with the timestamp in the 0 position and the data in the 1 position.

In order to only output position 1 of the timestamp-data array, you simply need to reach further into the array of arrays and pull out the information in position 2 of the first(only) array only. For example, if you put rpcresponse[0].result into a new variable (for simplicity’s sake):

var result = rpcresponse[0].result;
//The next line of code in your example would return "Array of arrays: [1456369867,"hellow me"]"
console.log("Array of arrays: ", result[0]);
//The next line of code in your example would return "Data string: hellow me"
console.log("Data string: ", result[0][1]);

In this case, since “hellow me” is already a string, you would also have no need for the stringification and parsing methods that you are currently using.

In general, if you are working with response objects in Node it’s probably worth it to get a solid foundation in handling objects and arrays in Javascript. There are lots of great resources online, but I enjoy CodeAcademy (free interactive lessons, no videos): https://www.codecademy.com/learn/javascript

Let us know if you have any further questions!

Thanks,
Michelle


#3

Hi Michelle,

Thank you for the reply. It works… I was googling for this answer for quite sometimes but couldnot get the answer.

Thanks for the link too.

Handi