更新时间:2023-11-27 22:08:46
在这里很难说出您要完成的工作,但这是一个开始 观点.我所做的主要更改是删除了两行
It's hard to tell what you are trying to accomplish here but here is a starting point. The main change I made was removing the two lines
// if true redirect
res.render('new-sensor-settings', { username: req.session.username, id: 'home', brand: brand })
因为那些是在回调之前执行的
db.collection.findOne
,因此当
内部res.render
被调用.我只是猜测这些行是
无关紧要的,也许是该函数先前迭代的遗留物.如果
它们不是偶然的,请提供有关该功能的更多信息
应该做.
because those were being executed before the callback of
db.collection.findOne
, and thus prevented a response from being sent when the
inner res.render
was called. I am just guessing that these lines are
extraneous, perhaps a leftover from a previous iteration of the function. If
they are not accidental, please provide more information about what the function
should do.
我进行的其他较小更改:
Other minor changes I made:
缩进2个空格.
我在if(!req.query.sid)
语句周围添加了花括号. (对
有没有大括号的单行if
语句,但是更少
可维护的,所以我个人的喜好是始终添加它们.)
I added curly brackets around the if(!req.query.sid)
statement. (it's fine to
have one-line if
statements without the curly brackets, but it's less
maintainable, so my personal preference is to always add them.)
我在发送响应的语句之前添加了return
.自从
res.redirect
和res.render
语句都放在它们的末尾
各自的if
块,它们中的两个不再有任何执行的机会.
因此,这里的return
并不是完全必要的-只是我个人的
使用它们的偏好,这明确表明现在响应已经
发送后,此路由中不应再执行任何代码.
I added return
in front of the statements that send a response. Since the
res.redirect
and res.render
statements are all placed at the end of their
respective if
blocks, there is no longer any chance of two of them executing.
So the return
s here aren't strictly really necessary - it's just my personal
preference to use them, making it explicit that now that the response has been
sent, no further code should be executed in this route.
我将if(!result)
移到了附加到先前if(result)
I moved if(!result)
into an else block attached to the previous if(result)
exports.sensorsettings = function(req, res){
if (!req.session.username) {
// if false render
return res.render('login', { logo: 'img/owl.png', id: 'home', brand: brand })
} else {
if(!req.query.sid){ (return res.redirect('/dashboard')); }
// get sid information from database
db.collection('sensors').findOne({sid:req.query.sid}, function(err, result) {
console.log('this is result:' + result);
if (result) {
console.log('this is result:' + result.name);
// Problem here too:
result= JSON.stringify(result);
// result is now a string, trying to access properties will be undefined
return res.render('new-sensor-settings', { username: req.session.username, name:result.name,
ipaddress: result.ipaddress, desc: result.desc, snmpcom: result.snmpcom, snmpver: result.snmpver,
snmpport: result.snmpport, snmpifindex: result.snmpifindex, ncusername: result.ncusername,
ncport: result.ncport})
} else {
return res.redirect('/errors?err=db');
};
});
}
};