微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决[object Event] 类型的请求参数快速路由器

我目前正在开发一个 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] 字符串并且即使在正确呈现模板后请求也会继续加载。

解决方法

很难说,因为您没有发布其他相关代码 - 但我猜想在 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 - 但这不是最佳做法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。