玉器服务器端传递的变量在浏览器中为空 [英] jade server side passed variable is empty at browser
问题描述
我有一个奇怪的问题,那就是传递给玉器的变量在浏览器中是空的, 似乎传递的变量为null,但不是
I have a strange problem which is the passed variable to the jade is empty at the browser, it seems the passed variable is null but it isn't
nodejs代码(整个路由代码):
the nodejs code(the whole route code):
exports.sensorsettings = function(req, res){
if (!req.session.username) {
// if false render
res.render('login', { logo: 'img/owl.png', id: 'home', brand: brand })
} else {
if(!req.query.sid) (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);
result= JSON.stringify(result);
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})
};
if (!result) res.redirect('/errors?err=db');
});
// if true redirect
res.render('new-sensor-settings', { username: req.session.username, id: 'home', brand: brand })
}
};
玉器文件(整个表格):
jade file(the whole form):
form.form-horizontal
.tab-content(style='padding: 0;')
#account-details.tab-pane
.row-fluid
.span6
.control-group
label.control-label Community
.controls
input#s_snmpcom.span10(type='text', value=snmpcom)
span.btn-action.single.glyphicons.circle_question_mark(style='margin: 0;', data-toggle='tooltip', data-placement='top', data-original-title='Specify your device Public Community name (For SNMP version 2c)')
i
.control-group
label.control-label Port Number
.controls
input#s_snmpport.input-mini(type='text', value=snmpport)
span.btn-action.single.glyphicons.circle_question_mark(style='margin: 0;', data-toggle='tooltip', data-placement='top', data-original-title='Specify your device SNMP port number, Default number is 161 ')
i
.span6
.control-group
label.control-label Version
.controls
select#s_snmpver.selectpicker.span6(data-style='btn-default', style='display: none;')
option 1
option 2c
option 3
.control-group
label.control-label IfIndex
.controls
input#s_snmpif.input-mini(type='text', value=snmpifindex)
span.btn-action.single.glyphicons.circle_question_mark(style='margin: 0;', data-toggle='tooltip', data-placement='top', data-original-title='Specify your SNMP interface index number.it could be found at interface details of your device')
i
hr.separator.bottom
.row-fluid
.span6
.control-group
label.control-label Username
.controls
input#s_ncuser.span10(type='text', value=ncusername)
span.btn-action.single.glyphicons.circle_question_mark(style='margin: 0;', data-toggle='tooltip', data-placement='top', data-original-title='Specify your device Netconf username, it could be on of defined username on your device with readonly access.')
i
.control-group
label.control-label Port Number
.controls
input#s_ncport.input-mini(type='text', value=ncport)
span.btn-action.single.glyphicons.circle_question_mark(style='margin: 0;', data-toggle='tooltip', data-placement='top', data-original-title='Specify your device ssh port number, Default number is 22 ')
i
.span6
.control-group
label.control-label password
.controls
input#s_ncpass.span10(type='password', value='')
span.btn-action.single.glyphicons.circle_question_mark(style='margin: 0;', data-toggle='tooltip', data-placement='top', data-original-title='Enter the password of Netconf username')
i
.form-actions(style='margin: 0;')
button.btn.btn-icon.btn-primary.glyphicons.circle_ok.pull-right(type='submit')
i
| Save changes
button.btn.btn-icon.btn-default.glyphicons.circle_remove.pull-right(type='button')
i
| Cancel
#account-settings.tab-pane.active
.row-fluid
.span3
strong Sensor General Settings
p.muted Configure Sensor basic settings here,all fields are required.
.span9
label(for='s_name') Name
input#s_name.span10(type='text', value=name)
span.btn-action.single.glyphicons.circle_question_mark(style='margin: 0;', data-toggle='tooltip', data-placement='top', data-original-title='Give your sensor a name so you could call it!')
i
.separator
label(for='s_ip') IP Address
input#s_ip.span10(type='text', value=ipaddress , placeholder='' )
span.btn-action.single.glyphicons.circle_question_mark(style='margin: 0;', data-toggle='tooltip', data-placement='top', data-original-title='Enter sensor IP address here')
i
.separator
label(for='s_offline') Sensor Offline
input#inputPasswordNew.span12(type='text', value='', placeholder='if your sensor is down for maintenance, change its status to offline')
.separator
label(for='s_description') Description
input#s_description.span12(type='text', value=desc)
.separator
.form-actions(style='margin: 0; padding-right: 0;')
button.btn.btn-icon.btn-primary.glyphicons.circle_ok.pull-right(type='submit')
i
| Save changes
注意:除username变量之外的所有内容均不起作用.我试图为这些变量分配一个静态值(例如,名称:"test"),但是这两种方法都不起作用.
note: everything except the username variable will not work. i tried to assign a static value for those variables (for example name:'test') but it won't work neither.
推荐答案
在这里很难说出您要完成的工作,但这是一个开始 观点.我所做的主要更改是删除了两行
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');
};
});
}
};
这篇关于玉器服务器端传递的变量在浏览器中为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!