-2

I am currently developing a NodeJS express app to manage customers. Within this app, I have the following route:

router.get('/customers/details/:customerID', accessControl.grantAccess('readAny', 'customer'), customerController.showCustomerDetails);

Now everytime I access req.params.customerID within my controller, the result is shown as "[object Event]" instead of the customer's id. So when I, for instance, open the route "customers/details/1", req.params.customerID is shown as [object Event] instead of my expected value "1".

Does anyone have an idea why this is the case?

Thank you!

Update: This is the relevant part of the controller:

showCustomerDetails: (req, res) => {
        let customerID = req.params.customerID;
        let customerPromise = db.customer.findOne({
            where: {
                id: customerID
            },
            include: [{
                model: db.document,
                as: 'documents',
            }, {
                model: db.file,
                as: 'files',
            }, {
                model: db.contactoption,
                as: 'contactOptions',
            }, {
                model: db.contactperson,
                as: 'contactPersons',
                include: {
                    model: db.contactoption,
                    as: 'contactOption'
                }
            }, {
                model: db.object,
                as: 'objects',
            }, {
                model: db.objectpart,
                as: 'objectParts',
            }, {
                model: db.address,
                as: 'address',
            }, {
                model: db.customer,
                as: 'parentCustomer',
            }]
        });
        let subcustomersPromise = db.customer.findAll({
            attributes: ['id', 'firstName', 'lastName', 'company', 'fullName'],
            where: {
                parentCustomerId: customerID
            }
        });

        Promise.all([customerPromise, subcustomersPromise]).then(results => {
            let customer = results[0];
            let subcustomers = results[1];
            let relevantCustomerIDs = subcustomers.map(subcustomer => subcustomer.id);
            relevantCustomerIDs.push(customer.id);

            db.protocol.findAll({
                include: [{
                    model: db.user,
                    as: 'targetUsers'
                }, {
                    model: db.user,
                    as: 'creator'
                }],
                where: {
                    refCustomerId: relevantCustomerIDs
                },
                order: [['createdAt', 'desc']]
            }).then(protocols => {
                customer.protocols = protocols;
                customer.subcustomers = subcustomers;
                if (customer) {
                    res.render('customers/customer-show', { customer: customer });
                } else {
                    req.flash('error', 'Der gesuchte Kunde konnte nicht gefunden werden.');
                    res.redirect('/customers');
                }
            }).catch(error => {
                console.log(error);
                req.flash('error', 'Beim Aufrufen der Kundenprotokolle ist ein Fehler aufgetreten, bitte versuchen Sie es erneut.');
                res.redirect('/customers');
            });
        }).catch(error => {
                console.log(error);
                req.flash('error', 'Beim Aufrufen des Kunden ist ein Fehler aufgetreten, bitte versuchen Sie es erneut.');
                res.redirect('/customers');
            });
    },

It does show the correct customer, so it somehow does have the correct value, however it only logs the [object event] string and the request keeps loading even after rendering the template correctly.

Max
  • 207
  • 1
  • 6
  • 20
  • 3
    The code you posted doesn't have any issues. Without knowing more details, it's hard to say what the problem could be. Do you have any middleware that could be changing the params? How are you calling your server? – nVitius Feb 14 '21 at 20:16
  • You also don't say _where_ you are logging it out, please include **all** relevant code. – James Feb 17 '21 at 21:07
  • I tried logging it directly at the top of the controller (right after 'let customerID = req.params.customerID;' in the code posted above). – Max Feb 19 '21 at 09:40
  • I tracked the issue down more: The issue only appears, if a file is associated to the customer. If I delete the file or remove the file include, there's no issue. I'll add relevant file code. – Max Feb 19 '21 at 10:42
  • after this line `let customerID = req.params.customerID;` what are the values of these 2 console logs `console.log(req.params.customerID)` and `console.dir(req.params, {depth: null})` – aRvi Feb 21 '21 at 05:25

1 Answers1

1

Hard to say since you didn't post the other relevant code - but I would guess something is happening in accessControl or customerController that's causing this. To ensure this is the case, I would test this and see if it can log the ID:

router.get('/customers/details/:customerID', (req, res) => {
  console.log(req.params.customerID)
  return res.status(200).send('OK')
});

You should also be able to get the ID via req.originalUrl.split('/').pop() - this is not best practice however.

  • Tested this and something really weird happens: The result of the console log for the customerID param also shows "[object Event]", however the ressource (the customer with the ID that is part of the URL) is shown correctly. I temporarily removed the accessControl without any effect. I will update the post with some more, possibly relevant code later. – Max Feb 17 '21 at 21:33
  • When trying your code, it does correctly log the param, so you're likely correct that the error is in the controller. I updated the OP with the controller code. – Max Feb 19 '21 at 09:56