How do I pass my url params to async functions in javascript files separate from the route with Node.js?

42
April 16, 2019, at 03:50 AM

I'm just learning to code and I'm building an application for my business. I'm having an issue with passing url parameters on my show route.

I'm using Node.js and MySQL. The route is displaying 7 different reports and I broke each report into separate js files, exported the functions and am calling the functions on the show route to display the page. The problem is that my MySQL queries on each function are not dynamic because I can't access the url parameters in each of these separate js files.

I've tried creating a function on the routes.js page but the request is undefined as each req is declared within a route. I also tried using ${req.params.id} on the individual js files, but req is still undefined.

Here is my show route:

router.get("/clients/:id/reports/monthlyreport/:marketplace/:month", function (req, res){
monthToMonth(function(arr){
    topSkuUnitsMonth(function(arr1){
        topSkuCogsMonth(function(arr2){
            productMix(function(arr3){
                ltmCogs(function(arr4){
                    topSkuCogsLTM(function(arr5){
                        topSkuUnitsLTM(function(arr6){
                            quarterComparison(function(arr7){
                                res.render("reports/show", {current: arr, math: math, units: arr1, cogs: arr2, mix: arr3, totals: arr4, yearCOGS: arr5, yearUnits: arr6, quarter: arr7});
                            })  
                        })
                    })
                })  
            })
        })  
    })
})  

});

Here is the monthToMonth function:

module.exports = function monthToMonth(callback){
var q = `select 
    t1.client_id,
    DATE_FORMAT(t1.period, '%Y-%m') as period,
    ROUND(t1.shipped_COGS,0) as shipped_COGS_current_month, 
    t1.shipped_units as shipped_units_current_month,
    t1.product_title as product_title_current_month, 
    t1.asin as asin_current_month,
    ROUND(t2.shipped_COGS,0) as shipped_COGS_past_month, 
    t2.shipped_units as shipped_units_past_month, 
    t2.product_title as product_title_past_month, 
    t2.asin as asin_past_month
    from all_months_ca t1
    join all_months_ca t2 on t1.asin = t2.asin
    where t1.client_id = 1 && t2.client_id  = 1 && (t1.shipped_units > 0 || t2.shipped_units > 0) && (DATE_FORMAT(t1.period, '%Y-%m') = '2019-05' && DATE_FORMAT(t2.period, '%Y-%m') = '2019-04')
    group by t1.asin
    order by shipped_COGS_current_month DESC;`
db.query(q, function(err, foundData){
    if(err) throw err;
    callback (foundData)
})

}

The MySQL query currently has the period and client_id hardcoded in the WHERE clause, but I need the url params to make the queries dynamic. How can I pass the parameters to these js. files?

READ ALSO
MySql combine records from same table

MySql combine records from same table

I'm attempting to preform a sql command to update rows, and combine rows with matching data

36
How to query between date and specific time range

How to query between date and specific time range

I need help on a query between date and specific time rangeFor example, a user entering the building from 1 Jan 2019 until 1 April 2019 and only from 7 PM until 10 PM for that date

39
How to invert or filter out the two employees that work on projects in other departments?

How to invert or filter out the two employees that work on projects in other departments?

So right now I am so close but right now the result table that is return only shows the employees that I don't want to see and I want to show the employees that only work on projects within their department

25
Is there a way to hide all database info such as password, username, etc?

Is there a way to hide all database info such as password, username, etc?

I'm creating several php scriptsMust I always insert the host, username, password, etc

31