0

When I set customer_id = 10001, it returns the response I want.

Using the same number (eg 10001), if I comment/remove the above code and set customer_id = req.body.customerNo, it returns '[]'.

Any idea why the first one works and not the second?

using MongoDB v4.0.5

router.get('/customer/invoice-balance/', function (req, res) {

    var customer_id = 10001;  // ************ works

    var customer_id = req.body.customerNo;  // ******** doesn't work
    console.log(customer_id)   // output --->  10001 

    var balanceTotal = function (customer_id, callback) {
        Transactions.aggregate([{
                    $match: {
                        customerNo: customer_id,
                        status: "Completed"

                    }
                },
                {
                    $group: {
                        _id: {
                            date: {
                                $dateToString: {
                                    format: "%m-%d-%Y",
                                    date: "$shippedDate"
                                }
                            },
                            customer: "$customerNo",
                            total: {
                                $sum: "$extAmount"
                            }
                        }
                    }
                },
                {
                    $sort: {
                        "_id.date": 1
                    }
                }

            ],
            function (err, results) {
                console.log("this is the result: ", results);
                callback(err, results);
            });
    };

    balanceTotal(customer_id, function (err, results) {
        if (err) {
            res.send(err);
        }
        res.json(results);
    });
});

updated fix:

router.get('/customer/invoice-balance/:customerNo', function (req, res) {

    var customer_id = parseInt(req.params.customerNo);
MBrewers
  • 141
  • 10
  • `var customer_id = parseInt(req.body.customerNo);` Anything from "reqest" is a "string". Mongoose cannot do "autocasting" from schema types in an aggregation pipeline. Related: [Moongoose aggregate $match does not match id's](https://stackoverflow.com/q/36193289/2313887) – Neil Lunn Mar 03 '19 at 03:16
  • Thanks Neil, I've already tried that. The result is the same '[]' – MBrewers Mar 03 '19 at 03:19
  • 1
    Oh it's a GET! You should instead `router.get('/customer/invoice-balance/:customerNo'` and then `req.param.customerNo`. You don't access "body" in a GET request. But all the rest about "type casting" still applies. – Neil Lunn Mar 03 '19 at 03:22
  • That's how my original code was set up, if I hard coded the customer_id it works. I want to be able to enter the customerNo on the client side and return the response (which is why I changed it to body). Any other things I can try? – MBrewers Mar 03 '19 at 03:33
  • 1
    Please read the linked posts as those links are there to help you. `10001 != "10001"` is your essential problem. And the rest of the problem is that you should be reading and sending via `req.params` instead of `req.body`. That's what the three links ( third in my comment for context ) are there for. – Neil Lunn Mar 03 '19 at 03:40
  • Thanks Neil for pointing me in the right direction. I've added the adjustments to code that works. – MBrewers Mar 03 '19 at 04:03

0 Answers0