-3

This question might sound duplicate of the one here but my scenario is quiet different. I have getSystemIds.js file like this:

var system_ids_list = {};

var getSystemIDs = function(req, callback) {    
    var client = //creating an object using internally developed node library
    client.request('sql_parameter', {'Query.ID' : query_number},

                        function(err, req, res){

                                //Do some stuff to calculate necessary values

                                system_ids_list[key_value] = value_array;
                                i+= 1;
                            }
                           return(req, callback)
                        }
                    )
        };

module.exports = getSystemIDs;

Now, as shown in the answer in the link above, I am doing this in app.js

   appSSL.get('/sysids', function(req, res) {
    var sys_ids = system_ids_list(req, function(err, sys_ids) {
     res.render('sysids', sys_ids);
    })
});

I am not getting any specific error but the web page never loads as if something is stuck in the process or it does not know where to go next. Can someone help me figure out what would be the best way to do this?

Community
  • 1
  • 1
LearningAsIGo
  • 1,240
  • 2
  • 11
  • 23

1 Answers1

1

Your getSystemIds() function is never calling the callback that was passed to it so the caller of getSystemIds() never gets a result - thus nothing ever happens on the request.

Change it to this:

var system_ids_list = {};
var getSystemIDs = function (req, callback) {
    var client = //creating an object using internally developed node library
        client.request('sql_parameter', {'Query.ID': query_number}, function (err, req, res) {
            //Do some stuff to calculate necessary values
            system_ids_list[key_value] = value_array;
            i += 1;

            // call the callback now to communicate back the async results
            callback(null, system_ids_list);
        });
};
module.exports = getSystemIDs;

The way you have your code structured, system_ids_list will accumulate more and more values each time getSystemIDs() is called. That seems a bit of an odd way to structure things so I'm pointing that out in case that is not really what you intend.


Also, your getSystemIDs() function does not return anything so you should change this:

appSSL.get('/sysids', function(req, res) {
    var sys_ids = system_ids_list(req, function(err, sys_ids) {
        res.render('sysids', sys_ids);
    });
});

to this to make it less missleading about what is going on:

appSSL.get('/sysids', function(req, res) {
    system_ids_list(req, function(err, sys_ids) {
        res.render('sysids', sys_ids);
    });
});

And, if res.render() is from a system like ExpressJS, then you probably want to be passing an object and naming a template:

res.render('sometemplate.html', {sysids: sys_ids});

If you want system_ids_list to not accumulate values, but to return a fresh value each time, you can define it within your function like this:

var getSystemIDs = function (req, callback) {
    var system_ids_list = {};
    var client = //creating an object using internally developed node library
        client.request('sql_parameter', {'Query.ID': query_number}, function (err, req, res) {
            //Do some stuff to calculate necessary values
            system_ids_list[key_value] = value_array;
            i += 1;

            // call the callback now to communicate back the async results
            callback(null, system_ids_list);
        });
};
module.exports = getSystemIDs;
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thank you very much for explaining it in detail. I have a follow up question. While doing res.render ('sysids', {sysids:sys_ids}); how can I access key, values in 'sys_ids' object? Name of my jade (view) file is 'sysids'. – LearningAsIGo Dec 11 '15 at 23:06
  • @DotNetNewBie - Since that's a Jade question and different from your original question and I don't happen to know Jade, I'd suggest you ask a new question about that. – jfriend00 Dec 11 '15 at 23:20
  • I figured that out. thanks. Thanks to my bad programming, now i can see what you meant by "system_ids_list will accumulate more and more values each time getSystemIDs() is called." . Is there a way I can avoid it? Like each time getSystemIDs() call regenerates 'sys_ids_list' object? – LearningAsIGo Dec 11 '15 at 23:53
  • 1
    @DotNetNewBie - You can just define `system_ids_list` inside your function so it gets initialized from scratch each time (see example I added to the end of my answer). – jfriend00 Dec 12 '15 at 00:14