[object Event] 类型的请求参数(快速路由器)

时间:2021-02-12 13:08:50

标签: node.js express express-router

我目前正在开发一个 NodeJS express 应用程序来管理客户。 在这个应用程序中,我有以下路线:

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

现在每次我在控制器中访问 req.params.customerID 时,结果都会显示为“[object Event]”而不是客户的 ID。 因此,例如,当我打开路由“customers/details/1”时,req.params.customerID 显示为 [object Event] 而不是我的预期值“1”。

有人知道为什么会这样吗?

谢谢!

更新: 这是控制器的相关部分:

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

它确实显示了正确的客户,所以它以某种方式确实具有正确的值,但是它只记录 [object event] 字符串并且即使在正确呈现模板后请求也会继续加载。

1 个答案:

答案 0 :(得分:1)

很难说,因为您没有发布其他相关代码 - 但我猜想在 accessControlcustomerController 中发生了一些事情,导致了这种情况。为了确保是这种情况,我会对此进行测试,看看它是否可以记录 ID:

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

您还应该能够通过 req.originalUrl.split('/').pop() 获取 ID - 但这不是最佳做法。