2

我有一个奇怪的问题是传递给玉的变量在浏览器中是空的,似乎传递的变量是空的,但它不是

nodejs代码(整个路由代码):

    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 })

    }
};

玉文件(全格式):

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

注意:除了用户名变量之外的所有内容都不起作用。我试图为这些变量分配一个静态值(例如名称:'test'),但它也不起作用。

4

2 回答 2

2

很难说你在这里想要完成什么,但这是一个起点。我所做的主要更改是删除了这两行

// if true redirect
res.render('new-sensor-settings', { username: req.session.username, id: 'home', brand: brand })

因为那些是在回调之前执行的 db.collection.findOne,因此阻止了在调用内部时发送响应res.render。我只是猜测这些行是无关的,可能是函数先前迭代的遗留物。如果它们不是偶然的,请提供有关该功能应该做什么的更多信息。

我做的其他小改动:

重新缩进 2 个空格。

if(!req.query.sid)我在语句周围添加了大括号。if(没有大括号的单行语句很好,但它的可维护性较差,所以我个人的偏好是总是添加它们。)

return在发送响应的语句前面添加了。由于 res.redirectandres.render语句都放在各自if块的末尾,因此它们中的两个不再有任何执行的机会。所以return这里的 s 并不是绝对必要的——这只是我个人使用它们的偏好,明确表示既然已经发送了响应,则不应在此路由中执行进一步的代码。

我搬进if(!result)了一个附加到前一个的 else 块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');
      };
    });
  }
};
于 2013-10-16T13:25:58.553 回答
-2

试试这个符号#{ipaddress},每次都对我有用

于 2013-10-16T13:08:18.873 回答