Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:04:50+00:00 2026-06-01T09:04:50+00:00

So what I’m trying to do is loop over an array of javascript objects

  • 0

So what I’m trying to do is loop over an array of javascript objects (sensors), each with cmd and parser attributes, run each cmd in the system shell, parse its output with the appropriate parser function, and append the resulting string to the server’s res object.

The first issue is that I get an error claiming the current object has no method parser. I’ve tried a few different things and still can’t get it to recognize that attribute of the object.

The second issue, when I gave up and hardcoded the parse function inside the loop (which destroys my ability to add commands that need a different parser), is that because the output of each command is read in a callback function, the server runs res.end() before the callbacks return, causing an empty response to the browser.

I’m quite new to node and javascript, so I’m sure I’ve made some beginner mistakes, I just can’t seem to sort them out in my head. Any help would be appreciated.

// requires
var http = require('http');
var exec = require('child_process').exec;

// parsers
var parseTemp = function(str) {
    return ((parseInt(str, 16) / 50 - 273.15).toFixed(2));
};

// sensors
var sensors = [
    {
        label:  "Object temp",
        cmd:    "i2cget -y 3 0x5a 0x07 w",
        parser: parseTemp,
        units:  " degrees C"
    },
    {
        label:  "Ambient temp",
        cmd:    "i2cget -y 3 0x5a 0x06 w",
        parser: parseTemp,
        units:  " degrees C"
    }
];

// server
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    for (var s in sensors) {
        exec(s.cmd, function (error, stdout, stderr) {
            res.write( s.label + ': ' + s.parser(stdout) + s.units + '\n' );
        });
    }
    res.end();
}).listen(1337, '');

console.log('Server running on port 1337');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:04:51+00:00Added an answer on June 1, 2026 at 9:04 am

    Your first issue is a result of each callback function referencing the same s variable. You can easily solve this by changing the for(s in sensors) loop into a sensors.forEach(function(s){...}). That will close over (google javascript closure) the s and make it the right one for each callback when the time comes. There might be other problems – I didn’t look all that hard once I saw this issue.

    Your second issue is precisely what you said. The res.end() is being called before the callbacks – since they’re asynchronous, so at best will be called after the current “thread” of execution is finished. You can solve that with a simple counter: increment before every exec call, and on each callback decrement and check if the counter has reached its initial value – if so, then run res.end().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.