<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Multiple requests within one function]]></title><description><![CDATA[<p dir="auto">Hello community :-)</p>
<p dir="auto">I’m kinda despairing with this problem :-(<br />
The idea: requesting the system temperatures of all raspberrys listed in the config file (they are all running a flask server)<br />
But I can’t get the object working.</p>
<p dir="auto">node_helper gets the object from my config file:</p>
<pre><code> {
                        module: 'getTemps',
                        header: 'RPi Temperatures',
                        position: 'top_right',
                        config: {
                                 devices: [
                                             {
                                                  url: 'http://10.0.0.83:2222/temp',
                                                  name: 'Alarmpi'
                                             },
                                             {
                                                  url: 'http://10.0.0.25:2222/temp',
                                                  name: 'Mediapi'
                                             },
                                             {
                                                  url: 'http://10.0.0.25:2222/temp',
                                                  name: 'Camerapi'
                                             }
                                ]
                        }
                },
</code></pre>
<p dir="auto">my node_helper.js:</p>
<pre><code>    getJSON: function(object) {
        var self = this;
        var data = object;
        console.log(data);
        for (var key in data) {
                console.log(data[key]);
                request({url: data[key].url, method: 'GET'}, function(error, response, body)
                {
                        if (!error &amp;&amp; response.statusCode == 200)
                        {
                                data[key].temp=body;
                                console.log(body);
                                console.log(data);
                        }
                        else
                        {
                                console.log(" Error: " + response.statusCode);
                                data[key].temp = 'offline';
                        }
                });
        }
        console.log(data);
        self.sendSocketNotification('received_values', data);
    }
</code></pre>
<p dir="auto">just one run creates two outputs and the final content of “data” is missing the temperature values.<br />
And as shown in the console output, the received temperature value is always added (it looks like temporary) to the last object in the list.</p>
<p dir="auto">[ { url: ‘<a href="http://10.0.0.83:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.83:2222/temp</a>’, name: ‘Alarmpi’ },<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’, name: ‘Mediapi’ },<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’, name: ‘Camerapi’ } ]<br />
{ url: ‘<a href="http://10.0.0.83:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.83:2222/temp</a>’, name: ‘Alarmpi’ }<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’, name: ‘Mediapi’ }<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’, name: ‘Camerapi’ }<br />
[ { url: ‘<a href="http://10.0.0.83:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.83:2222/temp</a>’, name: ‘Alarmpi’ },<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’, name: ‘Mediapi’ },<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’, name: ‘Camerapi’ } ]<br />
51.0<br />
[ { url: ‘<a href="http://10.0.0.83:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.83:2222/temp</a>’, name: ‘Alarmpi’ },<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’, name: ‘Mediapi’ },<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’,<br />
name: ‘Camerapi’,<br />
temp: ‘51.0’ } ]<br />
51.5<br />
[ { url: ‘<a href="http://10.0.0.83:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.83:2222/temp</a>’, name: ‘Alarmpi’ },<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’, name: ‘Mediapi’ },<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’,<br />
name: ‘Camerapi’,<br />
temp: ‘51.5’ } ]<br />
59.1<br />
[ { url: ‘<a href="http://10.0.0.83:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.83:2222/temp</a>’, name: ‘Alarmpi’ },<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’, name: ‘Mediapi’ },<br />
{ url: ‘<a href="http://10.0.0.25:2222/temp" target="_blank" rel="noopener noreferrer nofollow ugc">http://10.0.0.25:2222/temp</a>’,<br />
name: ‘Camerapi’,<br />
temp: ‘59.1’ } ]</p>
<p dir="auto">(Mediapi and Camerapi have the same IP in this example)</p>
<p dir="auto">thanks for your help :-)</p>
]]></description><link>https://forum.magicmirror.builders/topic/2441/multiple-requests-within-one-function</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Apr 2026 20:50:58 GMT</lastBuildDate><atom:link href="https://forum.magicmirror.builders/topic/2441.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 28 Apr 2017 16:17:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Multiple requests within one function on Sat, 29 Apr 2017 16:49:17 GMT]]></title><description><![CDATA[<p dir="auto">yeey, I got it working, although I don’t know why :P</p>
<pre><code> var NodeHelper = require("node_helper");
 var request = require('request');
 var Promise = require("promise");
 
 var result = new Array();
 
 module.exports = NodeHelper.create({
    start: function() {
        console.log("Starting node_helper for module [" + this.name + "]");
    },
    // subclass socketNotificationReceived
    socketNotificationReceived: function(notification, payload){
        if (notification === 'get_values') {
            this.getJSON(payload);
        }
    },
		
		
	requestp: function(url, key, name) {
		return new Promise(function (resolve, reject) {
			request({url:url, method: 'GET'}, function (err, res, body) {
				if (err) {
					return reject(err);
				} else if (res.statusCode !== 200) {
					err = new Error("Unexpected status code: " + res.statusCode);
					err.res = res;
					return reject(err);
				}
				
				result.push({url: url, name: name, temp: body});
				resolve(body);
			});
		});
	},
		
    getJSON: function(object) {		
        var self = this;
		
		for (var key in object) {
			//console.log(object[key]);
			
			self.requestp(object[key].url, key, object[key].name).then(function (info) {
					//console.log(info);
				}, function (err) {
					console.error("%s; %s", err.message, url);
					console.log("%j", err.res.statusCode);
			});
			
		}
		//console.log(result);
		self.sendSocketNotification('received_values', result);
		result = [];
    }
 });
</code></pre>
<p dir="auto">I wasn’t able to add a “temp” property to the original object - it was actually added, but it did not want to work outside the .then function :(</p>
]]></description><link>https://forum.magicmirror.builders/post/20748</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/20748</guid><dc:creator><![CDATA[blaukon]]></dc:creator><pubDate>Sat, 29 Apr 2017 16:49:17 GMT</pubDate></item><item><title><![CDATA[Reply to Multiple requests within one function on Fri, 28 Apr 2017 18:23:25 GMT]]></title><description><![CDATA[<p dir="auto">As @strawberry-3-141 mentioned, the <code>sendSocketNotification()</code> function is called, before the callback function of your request is execuded and the content of <code>data</code> is created.</p>
<p dir="auto">You could either call the <code>sendSocketNotification()</code> function inside the callback function of the request or put the <code>data</code> variable into a promise (which is a much more elegant approach and produces good-readable asynchronous code).</p>
<p dir="auto">There are good explanations for promises, just google “promises simple explanation” (or similar).<br />
I can recommend this one: <a href="https://coligo.io/javascript-promises-plain-simple/" target="_blank" rel="noopener noreferrer nofollow ugc">https://coligo.io/javascript-promises-plain-simple/</a></p>
<p dir="auto">Edit:<br />
Oh, and I would recommend the Bluebird.js library for promises. <a href="http://bluebirdjs.com/" target="_blank" rel="noopener noreferrer nofollow ugc">http://bluebirdjs.com/</a></p>
]]></description><link>https://forum.magicmirror.builders/post/20713</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/20713</guid><dc:creator><![CDATA[Beh]]></dc:creator><pubDate>Fri, 28 Apr 2017 18:23:25 GMT</pubDate></item><item><title><![CDATA[Reply to Multiple requests within one function on Fri, 28 Apr 2017 17:46:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/blaukon" aria-label="Profile: blaukon">@<bdi>blaukon</bdi></a> you assume that you have the value data already when you send the variable data to your module, but request is asynchronous and therefore you get most likely the data after you send the result</p>
]]></description><link>https://forum.magicmirror.builders/post/20711</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/20711</guid><dc:creator><![CDATA[strawberry 3.141]]></dc:creator><pubDate>Fri, 28 Apr 2017 17:46:03 GMT</pubDate></item></channel></rss>