Question

I am trying to get the list of all datastream inside a single feed and show its current value. I am using the documentation here and tutorial : http://xively.github.io/xively-js/docs/. I tried to write a simple javascript code below, but its not working.

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://d23cj0cdvyoxg0.cloudfront.net/xivelyjs-1.0.4.min.js"></script>
</head>
<body>
<h1>Xively Test</h1>
<h2>
<small>Datastream list</small>
</h2>
</p>
<script>
xively.setKey("yWYxyi3HpdqFCBtKHueTvOGoGROSAKxGRFAyQWk5d3JNdz0g");
xively.datastream.list(
    61916               //id from the tutorial example  
, function (data) {   
    data.results.forEach(function (datastream) {
        document.write("<br><br>Datastream ID:"+JSON.stringify(datastream["id"]
    document.write("<br><br>Current value:"+JSON.stringify(datastream["current_value"], null, 4));          
    });
}); 
</script>
</body>
</html>

Any idea why is it not working?

Thanks

Was it helpful?

Solution

There are some errors in your javascript

  1. There is no results object in your data parameter.
  2. Syntax error in your document.write statements

Try this code:

<script>
xively.setKey("yWYxyi3HpdqFCBtKHueTvOGoGROSAKxGRFAyQWk5d3JNdz0g");
xively.datastream.list(
    61916               //id from the tutorial example
, function (data) {
    data.forEach(function (datastream) {
        document.write("<br><br>Datastream ID:"+JSON.stringify(datastream["id"]));
        document.write("<br><br>Current value:"+JSON.stringify(datastream["current_value"], null, 4));
    });
});
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top