3

I have some code in node.js using express. I route a call to request data from a mysql database, and what I want to do is pass that to another function to restructure the returned json from a tabular form (table query) to a hierarchy type json.

I've tested the script separately to restructure the output from my sql query. However, I am having trouble passing it from my query function to my new script(function)

I'm just not seeing what I am doing wrong. Any help please and thanks.

exports.get_site_menu = function (req, res) {
    var dbc;
    console.log('In menu setup');
    async.waterfall([
        // get a connection

        function (callback) {
            db.db(callback);
        }

        ,
        querylookup, modifyjson
    ], completed);

    function querylookup(dbclient, res) {
        dbc = dbclient;
        dbc.query("SELECT categories, " +
            "subcategories, " +
            "pid, " +
            "title, " +
            "description " +
            "FROM MENU_SELECT_ACTIVE_VIEW " +
            "where company_id = ? and site_id = ?", [req.query.companyid, req.query.siteid], res);
    }

    function modifyjson(err, res) {

        categories = [];

        console.log('results ' + res);

        res.forEach(function (entry) {
            var cindex = categories.map(function (category) {
                return category.name;
            }).indexOf(entry.categories);

            console.log(cindex);
            if (cindex < 0) {
                // Not found in categories array
                cindex = categories.push({
                    name: entry.categories,
                    subcategories: []
                }) - 1; // -1 to fix the index
            }
            // Lets search the subcategory
            var category = categories[cindex];

            var sindex = category.subcategories.map(
                function (subcategory) {
                    return subcategory.name;
                }
            ).indexOf(entry.subcategories);

            if (sindex < 0) {
                // Not Found
                sindex = category.subcategories.push({
                    name: entry.subcategories,
                    items: []
                }) - 1;
            }
            // Subcategory exists. Just push
            category.subcategories[sindex].items.push({
                pid: entry.pid,
                description: entry.description,
                title: entry.title
            });
        });

        menu = {
            menu: {
                categories: categories
            }
        };
        console.log('menu ' + menu);
    }


    function completed(err, menu, fields) {
        if (dbc) dbc.end();
        if (err) {
            callback(err);
        } else {
            console.log(menu);
            res.contentType('json');
            res.send(JSON.stringify(menu));
        }
    }
};

1 Answer 1

1

You need to pass each result to own callback to pass next function. I have refactored your code like;

exports.get_site_menu = function (req, res) {
    var dbc;
    console.log('In menu setup');
    async.waterfall([
        // get a connection

        function (callback) {
            db.db(callback, some_result);
        },
        function querylookup(dbclient, res, callback) {
            dbc = dbclient;
            dbc.query("SELECT categories, " +
                "subcategories, " +
                "pid, " +
                "title, " +
                "description " +
                "FROM MENU_SELECT_ACTIVE_VIEW " +
                "where company_id = ? and site_id = ?", [req.query.companyid, req.query.siteid], res);
                callback(null, result_from_queryLookup);
        },
        function modifyjson(err, res, callback) {

            categories = [];

            console.log('results ' + res);

            res.forEach(function (entry) {
                var cindex = categories.map(function (category) {
                    return category.name;
                }).indexOf(entry.categories);

                console.log(cindex);
                if (cindex < 0) {
                    // Not found in categories array
                    cindex = categories.push({
                        name: entry.categories,
                        subcategories: []
                    }) - 1; // -1 to fix the index
                }
                // Lets search the subcategory
                var category = categories[cindex];

                var sindex = category.subcategories.map(
                    function (subcategory) {
                        return subcategory.name;
                    }
                ).indexOf(entry.subcategories);

                if (sindex < 0) {
                    // Not Found
                    sindex = category.subcategories.push({
                        name: entry.subcategories,
                        items: []
                    }) - 1;
                }
                // Subcategory exists. Just push
                category.subcategories[sindex].items.push({
                    pid: entry.pid,
                    description: entry.description,
                    title: entry.title
                });
            });

            menu = {
                menu: {
                    categories: categories
                }
            };
            console.log('menu ' + menu);
            callback(null, menu, fields);
        }
    ], function completed(err, menu, fields) {
        if (dbc) dbc.end();
        if (err) {
            callback(err);
        } else {
            console.log(menu);
            res.contentType('json');
            res.send(JSON.stringify(menu));
        }
    });
};

Especially, be careful on callback parts.

Sign up to request clarification or add additional context in comments.

4 Comments

hmmm ok I see what you mean... I get this error now db.db(callback, some_result); ^ ReferenceError: some_result is not defined at async.waterfall.dbc (/Users/dpreston10/Dropbox/workspace/QwikAssistServer/data/getdata.js:119:29) at fn (/Users/dpreston10/Dropbox/workspace/QwikAssistServer/node_modules/async/lib/async.js:517:34) at /Users/dpreston10/Dropbox/workspace/QwikAssistServer/node_modules/async/li b/async.js:441:34 at process._tickCallback (node.js:415:13)
some_result is my temp variable. You need to replace it with your own. I assume, first function generates a value called some_result and I passed it to second function, and so on
Im sorry :-( I'm just not getting past it. My result from the query are not passing down no matter what variables I use. The only variables passed into the first function are the 2 inputs for the query.
However, waterfall does not work that way. You need to pass step by step down the functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.