javascript - Access variables passed from Jade files in another jade file -
i have nodejs project integrated mongodb. here have created jade file have student list taken database. against each student record, there edit button. when user clicks on edit button, needs redirect user jade(editstudent.jade
) file that'll display form user can edit student's record.
in order accomplish this, have created following table in 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, 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 edit button passes name argument & pops alert follows.
pop alert
and display name title of editstudent
page calling index.js
index.js
var express = require('express'); var router = express.router(); /* home page. */ router.get('/', function(req, res, next) { res.render('index', { title: 'express' }); }); /* student welcome page. */ router.get('/studentindex', function(req, res, next) { res.render('studentindex', { title: 'student portal' }); }); /*get 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}); }); }); /* editstudent page */ router.get('/editstudent', function(req, res) { res.render('editstudent', { title : gh}); }); module.exports = router;
but getting error mentioning that
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 can pass variable redirected page(editstudent.jade
) appreciated.
you should read gh
request path, because you're sending in path: /editstudent?gh=' + gh
router.get('/editstudent', function(req, res) { let gh = req.query.gh; res.render('editstudent', { title : gh}); });
Comments
Post a Comment