Node.js 类型为[对象事件]的请求参数(快速路由器)
我目前正在开发一个NodeJS express应用程序来管理客户。 在此应用程序中,我有以下路线:Node.js 类型为[对象事件]的请求参数(快速路由器),node.js,express,express-router,Node.js,Express,Express Router,我目前正在开发一个NodeJS express应用程序来管理客户。 在此应用程序中,我有以下路线: router.get('/customers/details/:customerID', accessControl.grantAccess('readAny', 'customer'), customerController.showCustomerDetails); 现在,每次我访问控制器内的req.params.customerID时,结果显示为“[object Event]”,而不是客
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]字符串,即使在正确呈现模板之后,请求也会继续加载。很难说,因为您没有发布其他相关代码-但我猜是在
accessControl
或customerController
中发生了什么事情导致了这种情况。为了确保这种情况,我将测试它,看看它是否可以记录ID:
router.get('/customers/details/:customerID',(req,res)=>{
控制台日志(请求参数customerID)
返回res.status(200).send('OK')
});代码>您发布的代码没有任何问题。如果不知道更多细节,很难说问题出在哪里。您是否有可能更改参数的中间件?您如何调用您的服务器?您也没有说您要将其注销到哪里,请包括所有相关代码。我尝试直接将其记录在控制器顶部(就在上面发布的代码中的“let customerID=req.params.customerID;”之后)。我进一步跟踪了该问题:仅当文件与客户关联时,才会出现该问题。如果我删除文件或删除包含的文件,则没有问题。我将添加相关的文件代码。在这一行之后let customerID=req.params.customerID
这两个控制台日志的值是什么console.log(req.params.customerID)
和console.dir(req.params,{depth:null})
测试了这一点,发生了一些非常奇怪的事情:customerID参数的控制台日志的结果也显示“[object Event]”,但是ressource(ID为URL一部分的客户)已正确显示。我暂时删除了accessControl,但没有任何效果。稍后我将使用更多可能相关的代码更新帖子。在尝试您的代码时,它确实正确记录了参数,因此您很可能更正了控制器中的错误。我用控制器代码更新了OP。