Nayantara Jeyaraj
Nayantara Jeyaraj

Reputation: 2716

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

output

When clicking on the edit button it passes the name as an argument & initially pops up an alert as follows.

Pop up Alert

popup

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 not defined

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.

Upvotes: 0

Views: 735

Answers (2)

alexmac
alexmac

Reputation: 19587

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

Upvotes: 2

Vishnu
Vishnu

Reputation: 12283

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

Upvotes: 1

Related Questions