简体   繁体   中英

Access variables passed from Jade files in another jade file

I have a nodejs project integrated with mongodb. 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.

In order to accomplish this, I have created the following table in my studentList.jade file.

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

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

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.

You should read gh from request path, because you're sending it in a path: /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. get the gh from the query params

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM