[英]Access variables passed from Jade files in another jade file
I have a nodejs project integrated with mongodb. 我有一个与mongodb集成的nodejs项目。 Here I have created a jade file where I have a student list taken from the database.
在这里,我创建了一个玉文件,其中有一个从数据库中获取的学生名单。 Against each student record, there is an edit button.
针对每个学生记录,都有一个编辑按钮。 So when the user clicks on the edit button, it needs to redirect the user to another jade(
editstudent.jade
) file that'll display a form where the user can edit that student's record. 因此,当用户单击编辑按钮时,它需要将用户重定向到另一个jade(
editstudent.jade
)文件,该文件将显示一个表单,用户可以在其中编辑该学生的记录。
In order to accomplish this, I have created the following table in my studentList.jade
file. 为此,我在
studentList.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 产量
When clicking on the edit button it passes the name as an argument & initially pops up an alert as follows. 单击编辑按钮时,它将名称作为参数传递,并首先弹出如下警告。
Pop up Alert 弹出警报
And I would like to display this name as the Title of the editstudent
Page that I am calling from the index.js 我想将此名称显示为我从index.js调用的
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;
But I am currently getting the error mentioning that 但是我目前收到错误消息
gh is not defined
gh未定义
editstudent.jade editstudent.jade
extends layout
block content
h1 = title
p Welcome editor
button(onclick = 'backToList()')="Back"
script.
function backToList() {
window.location.href = '/studentList'
}
Any suggestions on how I can pass this variable to my redirected page( editstudent.jade
) will be appreciated. 关于如何将此变量传递到重定向页面(
editstudent.jade
)的任何建议将不胜感激。
You should read gh
from request path, because you're sending it in a path: /editstudent?gh=' + gh
您应该从请求路径中读取
gh
,因为您是通过以下路径发送它的: /editstudent?gh=' + gh
router.get('/editstudent', function(req, res) {
let gh = req.query.gh;
res.render('editstudent', { title : gh});
});
gh
is not defined in your code. gh
在您的代码中未定义。 get the gh
from the query params 从查询参数中获取
gh
/* GET the editStudent page */
router.get('/editstudent', function(req, res) {
res.render('editstudent', { title : req.query.gh});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.