Javascript (MongoDB Atlas+;ExpressJS)如何使用路由将数据从一个.ejs文件发送到另一个文件,以便将数据加载到新网页上?
我正在做一个项目,在这个项目中,我使用expressJS(和mongoose)在模拟大学招生系统的网站上读写MongoDB Atlas中的数据。我可以从给定集合中提取所有学生信息并将其显示在页面上,但我不能在页面之间发送信息。具体来说,我正在尝试将学生ID从所有学生档案(profiles.ejs)页面发送到学生详细信息(admisStudentView.ejs)页面,在该页面中我将只加载该人的信息。我能够接收正确学生的JSON,但我根本无法加载学生详细信息页面。当我尝试时,我得到一个错误:error[ERR\u HTTP\u HEADERS\u SENT]:发送到客户端后无法设置头 /视图/profiles.ejs:Javascript (MongoDB Atlas+;ExpressJS)如何使用路由将数据从一个.ejs文件发送到另一个文件,以便将数据加载到新网页上?,javascript,mongodb,express,ejs,mongodb-atlas,Javascript,Mongodb,Express,Ejs,Mongodb Atlas,我正在做一个项目,在这个项目中,我使用expressJS(和mongoose)在模拟大学招生系统的网站上读写MongoDB Atlas中的数据。我可以从给定集合中提取所有学生信息并将其显示在页面上,但我不能在页面之间发送信息。具体来说,我正在尝试将学生ID从所有学生档案(profiles.ejs)页面发送到学生详细信息(admisStudentView.ejs)页面,在该页面中我将只加载该人的信息。我能够接收正确学生的JSON,但我根本无法加载学生详细信息页面。当我尝试时,我得到一个错误:err
<!-- Currently this button is needed to load the student boxes, they do not display when the page first loads -->
<form action="/student/" method="get">
<button class="btn" type="submit">Refresh Applicant Pag</button>
</form>
<div class="database-tables">
<table id="Students" class="table" border="1" >
<% if(locals.student) {%>
<% for(var i = 0; i < student.length; i++) {%>
<td>
<div>
<center>
<footcaption>
<%= student[i].ApplicationStatus %><br>
<%= student[i].FirstName %>
<%= student[i].MiddleName %>
<%= student[i].LastName %><br>
<%= student[i]._id %><br>
<!-- In theory this button should load /views/admisStudentView with the relevent student's info-->
<form action="/student/detail" method="post">
<button type="submit" name="_id" value="<%= student[i]._id %>">
View Profile
<input type="hidden" name="_id" value="<%= student[i]._id %>" />
</button>
</form>
</footcaption>
</center>
</div>
</td>
<% } %>
<% } %>
</table>
</div>
<body>
<div class="content">
<div class="container">
<div class="row">
<div class="col-5" id="boxstudent">
<div class="row">
<div class="col" id="input">
<div class="row attribute" id="fname">
First Name:
</div>
...
</div>
<div class="col-4" id="and">
<% if(locals.student_details) {%>
<div class="row attribute" id="fname">
<%= student[i].FirstName %>
</div>
...
<% } %>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
var express = require('express');
var router = express.Router();
const app = express();
app.set('view engine', 'ejs');
var Student = require('../schema/StudentSchema');
// Get all students & info in the DB
router.get('/', getStudents, render);
function getStudents(req, res, next){
Student.find({}, function (err, student) {
console.log(student);
console.log("Testing")
res.locals.student = student;
next();
});
};
function render(req, res){
res.render("../views/profiles.ejs");
};
// Get info for a single student based on _id for admissions
router.post('/detail', getStudentDetails, renderAdmisStudentView);
function getStudentDetails (req, res, next){
Student.findOne({_id: req.body._id}, function(err, student_details){
res.locals.student_details = student_details;
console.log(student_details)
res.send();
next();
});
}
function renderAdmisStudentView(req, res){
res.render("../views/admisStudentView.ejs");
}
module.exports = router;