Click here to Skip to main content
15,886,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a small library that should run a couple of async operations and return an object containing the result of those operations but the interpreter either says the function is not a constructor or when it accepts the constructor, the operation returns an empty object. I think my problem has to do with my variable scoping but maybe it could be worse.

What I have tried:

The code itself looks like this
var render = {username: '', available: '', orders: '', frequent: ''},
attachModule = 1;

 function OnIndexLoad (username) {

this.endRes = function (username) {
    render.username = username;
    console.log(render)
    return render;
};

this.init = function (username, current) {
    var utilMap =    {

    1: function (index) {
        http.get({port: '1717', path: '/admin/', headers: {Accept: 'text/html'}}, function(res) {
        var temp = ''
        res.setEncoding('utf8');

        res.on('data', function (chunk) {
            temp += chunk;
        }).on('end', function() {
    // add result of async operation
            render.available = temp
            index++
        })

    }).on('error', function(e) {
            console.log(e)
        });
    },

    2: function(index) {
        http.get('http://localhost:1717/admin/page=20',  function(res) {

        var temp = ''
        res.setEncoding('utf8');

        res.on('data', function (chunk) {
            temp += chunk;
        }).on('end', function() {
    // add result of async operation
            render.orders = temp;
            index++
        })
    }).on('error', function(e) {
            console.log(e)
        });
    },

    3: function(index) {
        ordersModel.find({}, , function (err, orders) {
        if (err) throw err;

// ignore this whole bit but look out for the function's last line
        var hashMap = [], returnArr = [];

        orders.forEach(function (order) {
            hashMap.push(order.toObject()['food'].split(","));
        })

        hashMap.reduce(function(a, b) {
            return a.concat(b)
        }, []).forEach(function(item) {

            if ((k = returnArr.findIndex(function(elem) {
                return elem[0] == item;
            })) != -1) {
                returnArr[k][1]++;
            }
            else returnArr.push([item, 1]);
        })

        hashMap = [], returnArr = returnArr.sort(function(a, b) {
            return b[1] - a[1];
        }).slice(0, 5).forEach(function(elem) {
            hashMap.push({name: elem[0], counter: elem[1]})
        });
    // add result of async operation
        render.frequent = component("frequent", hashMap); // here
        index++;
    })
    }
    }; // close util map

// the important part
    var itemsToReturn = Object.keys(render).filter(e => e != 'username');
    utilMap[current](current);

    if (attachModule < itemsToReturn.length) {
            this.init(current, username) // does not recurse and incrementally perform async ops
        }
        else this.endRes(username);
}

this.init(attachModule, username)
}

exports = OnIndexLoad


Then, in a middleware from the calling script i.e the index.js requiring this library, I have,

var onIndexLoad = require("./lib/index-load");
new onIndexLoad(req.session.username);


I thought calling it that way will expose the loaded object for me--something like this

{key1: resultOfOperation1, key2: resultOfOperation2, key3: resultOfOperation3}

But calling it that way only exposes errors. How do I call the endRes only and only when the render object is ready to be exported?
Posted
Updated 9-Apr-17 18:41pm

1 solution

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
nmeri17 10-Apr-17 1:21am    
But I told you what the console says. The function is not a constructor. It doesn't even run let alone allow me observe any breakpoints. Besides, the code is server side js so I've got no devtools so to speak. The important thing here is that the function isn't running to start with.
Patrice T 10-Apr-17 1:32am    
You said:
"or when it accepts the constructor, the operation returns an empty object."
Look at:
Debugger | Node.js v7.8.0 Documentation[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900