2017-07-28 82 views
0

我有一个nodejs项目集成了mongodb。在这里,我创建了一个翡翠文件,其中我有一个从数据库中取得的学生名单。针对每个学生记录,都有一个编辑按钮。因此,当用户点击编辑按钮时,需要将用户重定向到另一个玉石(editstudent.jade)文件,该文件将显示一个表单,用户可以编辑该学生的记录。访问变量从另一个玉文件中的Jade文件传递

为了做到这一点,我在我的studentList.jade文件中创建了下表。

studentList.jade

extends layout 

block content 
    h1. 
     Student List 

    table 
     tr 
      th. 
       Name 
      th. 
       Age 
      th. 
       Contact 
      th. 
       Edit Student 
      th. 
       Delete Student 
     each student, i in studentlist 
      tr 
       td(id='studentname') #{student.name} 
       td #{student.age} 
       td 
        a(href="mailto:#{student.email}")=student.email 
       td 
        button(onclick='editstudent("#{student.name}")')= "Edit" 
       td 
        button(onclick='removestudent()')= "Remove" 
    script. 
     function editstudent(gh) { 
      alert("Edit "+gh+"'s Profile"); 
      window.location.href = '/editstudent?gh=' + gh; 
     } 

输出

output

当编辑按钮点击它把名作为参数&最初如下弹出一个警告。

拉网快讯

popup

而且我想显示此名称作为editstudent页,我从index.js

index.js调用的标题

var express = require('express'); 
var router = express.Router(); 

/* GET home page. */ 
router.get('/', function(req, res, next) { 
    res.render('index', { title: 'Express' }); 
}); 

/* GET student welcome page. */ 
router.get('/studentIndex', function(req, res, next) { 
    res.render('studentIndex', { title: 'Student Portal' }); 
}); 

/*GET the studentList page */ 
router.get('/studentList', function(req, res) { 
    var db = req.db; 
    var collection = db.get('studentcollection'); 
    collection.find({},{},function(e,docs){ 
     res.render ('studentList', { 
     "studentlist" : docs}); 
    }); 
}); 

/* GET the editStudent page */ 
router.get('/editstudent', function(req, res) { 
    res.render('editstudent', { title : gh}); 
}); 

module.exports = router; 

但我目前得到的错误提的是

GH没有定义

gh not defined

editstudent.jade

extends layout 

block content 
    h1 = title 
    p Welcome editor 
    button(onclick = 'backToList()')="Back" 

    script. 
     function backToList() { 
      window.location.href = '/studentList' 
     } 

我如何可以通过任何建议这个变量到我的重定向页面(editstudent.jade)将是appr eciated。

回答

1

您应该请求路径读gh,因为你在一个路径发送它:/editstudent?gh=' + gh

router.get('/editstudent', function(req, res) { 
    let gh = req.query.gh; 
    res.render('editstudent', { title : gh}); 
}); 
+0

非常感谢。有效 –

1

gh未在您的代码中定义。您可以通过查询gh PARAMS

/* GET the editStudent page */ 
router.get('/editstudent', function(req, res) { 
    res.render('editstudent', { title : req.query.gh}); 
}); 
+0

谢谢。得到它了! –

相关问题