Passing Node.js MySQL query results to the next function within an async.waterfall workflow

In my node.js code using express, I have set up a route to request data from a mysql database. My goal is to pass the returned JSON in tabular form to another function to restructure it into a hierarchy type JSON.

I have individually tested the script to restructure the SQL query output. However, I am facing difficulties passing it from my query function to the new script(function).

I can't seem to figure out what I am doing wrong. Any assistance would be greatly appreciated. Thank you.

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));
        }
    }
};

Answer №1

To ensure proper execution of each function in sequence, it is important to pass the result to its own callback for the next function to use. Your code has been revised as follows:

exports.get_site_menu = function (req, res) {
    var dbc;
    console.log('In menu setup');
    async.waterfall([
        // establish 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) {
                    // Check if not found in categories array
                    cindex = categories.push({
                        name: entry.categories,
                        subcategories: []
                    }) - 1; // Adjust index by -1
                }
                
                var category = categories[cindex];

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

                if (sindex < 0) {
                    // If subcategory not found
                    sindex = category.subcategories.push({
                        name: entry.subcategories,
                        items: []
                    }) - 1;
                }

                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));
        }
    });
};

Pay special attention to the callback segments.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps should I take to address the problems encountered when trying to launch an application in PM2 on a

My current project involves setting up Docusaurus on Windows Server 2016 with Node.js version 12.14.0 using PM2 on IIS10 for automatic app restarts after server reboots. Although new to Node.js, I followed the typical process of running npm run start withi ...

Having trouble uploading an image using Dropzone.js in conjunction with Node.js? Let us assist you with that!

Currently working on developing a web application using Express and Node.js. I am looking to enable users to upload profile pictures utilizing Dropzone.js. Here is what I have implemented so far: <script type="text/javascript"> Dropzone.options. ...

Express.js restricts the number of requests to a maximum of 6

I am facing an issue with my Flask server that streams image data using the multipart/x-mixed-replace header. The Express server is set up to connect to the Flask server, receive the image data, and then deliver it to the client also utilizing the multipar ...

Deployment of the API and frontend on separate subdomains and domains respectively within the Google Cloud Platform

My journey began by setting up a Google App Engine where I deployed both the API and the frontend on my custom domain, which I referred to as mysite.ms. The API was written in nodejs with Express, while the frontend was a React application. To achieve this ...

React approach for managing multiple combobox values

Currently, I'm working on a page where users can upload multiple files and then select the file type for each file from a dropdown menu before submitting. These 'reports' are the uploaded files that are displayed in rows, allowing users to c ...

Trying to execute `npm install anything` results in a failure due to ECONN

Having a massive issue with npm on a virtual machine in a data center where ECONNRESET fails keep occurring during installation processes. Tried switching from http to https in the registry settings, but it didn't fix anything. Even attempts to upgra ...

Is there a way to align the image next to the form and ensure they are both the same size?

I've been struggling to resize the image to match the form size, but I can't seem to get it right. Can anyone provide me with some guidance on this issue? I have obtained this contact form from a website that I plan to modify slightly, but I need ...

How to pass a String Array to a String literal in JavaScript

I need to pass an array of string values to a string literal in the following way Code : var arr = ['1','2556','3','4','5']; ... ... var output = ` <scr`+`ipt> window.stringArray = [`+ arr +`] & ...

Using PHP to extract information from a JSON file

After researching various articles and tutorials, I've managed to piece together the code below. However, as a beginner in PHP, JSON, and Javascript, I am seeking guidance. The task at hand is to update a div with the ID "playerName" every 10 seconds ...

What is the best way to incorporate autoplay video within the viewport?

My objective is for the video to automatically start playing when it enters the viewport, even if the play button is not clicked. It should also pause automatically when it leaves the viewport, without the need to click the pause button. <script src=& ...

Mongoose reverse population involves querying a document's references

I am currently exploring ways to populate my business orders using the businessId property in my orders collection. I attempted a solution but I am facing difficulties making it work. https://www.npmjs.com/package/mongoose-reverse-populate If you have an ...

Node.js and Express: Delivering Seamless Video Streaming via HTTP 206 Partial Content

Having a binary file like an mp4 video in a MarkLogic database, I am utilizing the Node.js API of the database to stream this document in segments. The structure is as follows: html document <video controls="controls" width="600"> <source src= ...

Manipulate the value(s) of a multi-select form field

How can you effectively manage multiple selections in a form field like the one below and manipulate the selected options? <select class="items" multiple="multiple" size="5"> <option value="apple">apple</option> <option va ...

An error occurred when attempting to run the command npm run compile:sass, displaying the message: npm ERR! missing script:

Everything seems to be in place with the sass folders and files, so what could be the issue? I have my package.json file set up correctly with the following code: { "name": "starter", "version": "1.0.0", " ...

eliminating items from an array nested inside another array

****************UPDATED********************************************************* I am stuck trying to manipulate an array within another array and remove elements based on conditions. The main goal is to make changes without altering the original array of ...

Transferring a file from the /tmp directory on Amazon Lambda to an S3 bucket using Node.js

My current project involves developing a straightforward screenshot program using Amazon Lambda. The program will take a snapshot of a specified URL and store it in JSON format like this: { "site": "www.example.com", "width": "320", "height": "480" ...

External CSS File causing improper sizing of canvas image

I have been working on implementing the HTML5 canvas element. To draw an image into the canvas, I am using the following javascript code: var img = new Image(); var canvas = this.e; var ctx = canvas.getContext('2d'); img.src = options.imageSrc; ...

Implementing automatic selection mode in Kendo MVC grid

Seeking to modify the SelectionMode of a Kendo MVC Grid, I aim to switch from single to multiple using Javascript or JQuery upon checkbox selection, and revert back when the checkbox is unchecked. Is this feasible? Additionally, I am successfully binding a ...

What is the best way to add a simple Search bar to the Material UI Data Grid component?

If we take a look at this data grid example: check it out here I'd like to incorporate a simple search bar similar to the one shown here Can someone provide guidance on how to add this feature to Data Grid MUI? ...

Switching the endpoint renders the middleware ineffective

I've encountered a puzzling issue with my NodeJs - Express server, which serves as the backend for my mobile application. The problem arises when I send post requests to certain endpoints like checkmail and checkusername using axios from the frontend ...