作业未定义
Job is not defined
我正在尝试创建一个申请人可以根据 he/she 想要的职位提交的申请表,但是如果我尝试点击申请表上的提交,我会收到如图所示的错误以下。我不知道该怎么做,我以为我已经弄清楚了一切,但我不确定我错过了什么。请帮忙,提前谢谢你。
路由器
const Job = require('../../models/Job');
router.get("/jobs", function(req, res){
Job.find()
.then(results => {
res.render('jobs/', { Job: results })
})
.catch(error => console.error(error))
});
//applicants handling
router.post("/jobs", function(req, res){
const { applicant, email, contact, address, position} = req.body;
let errors = [];
if(!applicant || !email || !contact || !address || !position){
errors.push({ msg: 'Please fill out all fields.' });
}
if(errors.length > 0 ){
res.render('jobs/',{
errors,
applicant,
email,
contact,
address,
position
});
}else{
Applicants.findOne({applicant: applicant})
.then(user => {
if(user){
errors.push({ msg: 'You already sent an application, no worries we will get back at you soon.' })
res.render('jobs/',{
errors,
applicant,
email,
contact,
address,
position
});
}else{
const newApplicants = new Applicants({
applicant,
info:{
email,
contact,
address,
},
position:{
position
}
});
newApplicants.save()
errors.push({ msg: 'Thank you for sending as a application notice, we will contact you soon.' })
}
});
}
});
views/jobs/index.ejs
...
<% for(var i=0; i < Job.length; i++) { %>
<div class="card">
<div class="card-body">
<h5 class="card-title"><%= Job[i].title %></h5>
<p class="card-text"><%= Job[i].description %></p>
<div class="text-muted mb-4">Requirements: <%= Job[i].requirements %></div>
<p>
<a class="btn btn-primary" data-toggle="collapse" href="#<%= Job[i].title%>" role="button" aria-expanded="false" aria-controls="collapseExample">
Apply Now
</a>
</p>
<div class="collapse" id="<%= Job[i].title %>">
<div class="card card-body">
<%- include('../_partial/messages') -%>
<h4>Applying for <%= Job[i].title %></h4>
<form action="/jobs" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">Full Name</label>
<input
type="text"
class="form-control"
id="applicant"
name="applicant"
value="<%- typeof applicant != 'undefined' ? applicant: '' -%>"
>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email Address</label>
<input
type="email"
class="form-control"
id="description"
name="description"
value="<%- typeof description != 'undefined' ? description: '' -%>"
>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Contact Number</label>
<input
type="text"
class="form-control"
id="contact"
name="contact"
value="<%- typeof contact != 'undefined' ? contact: '' -%>"
>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Complete Address</label>
<input
type="text"
class="form-control"
id="address"
name="address"
value="<%- typeof address != 'undefined' ? address: '' -%>"
>
</div>
<div class="form-group">
<input
type="hidden"
id="position"
name="position"
value="<%= Job[i].title %>"
>
</div>
<button type="submit" class="btn btn-primary">Submit Application Form</button>
</form>
</div>
</div>
</div>
<%if (i > 0) { %>
</div>
<% } %>
<% } %>
...
错误
ReferenceError: /home/redacted/Projects/redacted/views/jobs/index.ejs:12
10| </div>
11| <div class="container mb-5">
>> 12| <% for(var i=0; i < Job.length; i++) { %>
13| <div class="card">
14| <h5 class="card-header">Urgent</h5>
15| <div class="card-body">
Job is not defined
...
查看错误:
ReferenceError: /home/redacted/Projects/redacted/views/jobs/index.ejs:12
10| </div>
11| <div class="container mb-5">
>> 12| <% for(var i=0; i < Job.length; i++) { %>
您编写的 EJS 模板需要呈现变量 Job
才能存在。
防止此类错误的最简单方法是在 EJS 模板顶部添加一些代码,例如:
<%
locals.Job = locals.Job || [];
%>
这称为空合并,意味着如果提供了 none,我们将为 Job
提供默认值。 EJS 使用 locals
变量来保存您通常作为第二个参数传递给渲染函数的渲染数据,例如:
// In this example, locals = { Job: results };
res.render('jobs/', { Job: results })
我们通常不需要显式地写 locals.Job
并且通常可以在 EJS 模板文件中使用 Job
,除非之前没有定义它。
在您的示例中,正是这种情况导致了错误:
res.render('jobs/',{
errors,
applicant,
email,
contact,
address,
position
});
如您所见,第二个参数不包含 Job
键。
我正在尝试创建一个申请人可以根据 he/she 想要的职位提交的申请表,但是如果我尝试点击申请表上的提交,我会收到如图所示的错误以下。我不知道该怎么做,我以为我已经弄清楚了一切,但我不确定我错过了什么。请帮忙,提前谢谢你。
路由器
const Job = require('../../models/Job');
router.get("/jobs", function(req, res){
Job.find()
.then(results => {
res.render('jobs/', { Job: results })
})
.catch(error => console.error(error))
});
//applicants handling
router.post("/jobs", function(req, res){
const { applicant, email, contact, address, position} = req.body;
let errors = [];
if(!applicant || !email || !contact || !address || !position){
errors.push({ msg: 'Please fill out all fields.' });
}
if(errors.length > 0 ){
res.render('jobs/',{
errors,
applicant,
email,
contact,
address,
position
});
}else{
Applicants.findOne({applicant: applicant})
.then(user => {
if(user){
errors.push({ msg: 'You already sent an application, no worries we will get back at you soon.' })
res.render('jobs/',{
errors,
applicant,
email,
contact,
address,
position
});
}else{
const newApplicants = new Applicants({
applicant,
info:{
email,
contact,
address,
},
position:{
position
}
});
newApplicants.save()
errors.push({ msg: 'Thank you for sending as a application notice, we will contact you soon.' })
}
});
}
});
views/jobs/index.ejs
...
<% for(var i=0; i < Job.length; i++) { %>
<div class="card">
<div class="card-body">
<h5 class="card-title"><%= Job[i].title %></h5>
<p class="card-text"><%= Job[i].description %></p>
<div class="text-muted mb-4">Requirements: <%= Job[i].requirements %></div>
<p>
<a class="btn btn-primary" data-toggle="collapse" href="#<%= Job[i].title%>" role="button" aria-expanded="false" aria-controls="collapseExample">
Apply Now
</a>
</p>
<div class="collapse" id="<%= Job[i].title %>">
<div class="card card-body">
<%- include('../_partial/messages') -%>
<h4>Applying for <%= Job[i].title %></h4>
<form action="/jobs" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">Full Name</label>
<input
type="text"
class="form-control"
id="applicant"
name="applicant"
value="<%- typeof applicant != 'undefined' ? applicant: '' -%>"
>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email Address</label>
<input
type="email"
class="form-control"
id="description"
name="description"
value="<%- typeof description != 'undefined' ? description: '' -%>"
>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Contact Number</label>
<input
type="text"
class="form-control"
id="contact"
name="contact"
value="<%- typeof contact != 'undefined' ? contact: '' -%>"
>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Complete Address</label>
<input
type="text"
class="form-control"
id="address"
name="address"
value="<%- typeof address != 'undefined' ? address: '' -%>"
>
</div>
<div class="form-group">
<input
type="hidden"
id="position"
name="position"
value="<%= Job[i].title %>"
>
</div>
<button type="submit" class="btn btn-primary">Submit Application Form</button>
</form>
</div>
</div>
</div>
<%if (i > 0) { %>
</div>
<% } %>
<% } %>
...
错误
ReferenceError: /home/redacted/Projects/redacted/views/jobs/index.ejs:12
10| </div>
11| <div class="container mb-5">
>> 12| <% for(var i=0; i < Job.length; i++) { %>
13| <div class="card">
14| <h5 class="card-header">Urgent</h5>
15| <div class="card-body">
Job is not defined
...
查看错误:
ReferenceError: /home/redacted/Projects/redacted/views/jobs/index.ejs:12
10| </div>
11| <div class="container mb-5">
>> 12| <% for(var i=0; i < Job.length; i++) { %>
您编写的 EJS 模板需要呈现变量 Job
才能存在。
防止此类错误的最简单方法是在 EJS 模板顶部添加一些代码,例如:
<%
locals.Job = locals.Job || [];
%>
这称为空合并,意味着如果提供了 none,我们将为 Job
提供默认值。 EJS 使用 locals
变量来保存您通常作为第二个参数传递给渲染函数的渲染数据,例如:
// In this example, locals = { Job: results };
res.render('jobs/', { Job: results })
我们通常不需要显式地写 locals.Job
并且通常可以在 EJS 模板文件中使用 Job
,除非之前没有定义它。
在您的示例中,正是这种情况导致了错误:
res.render('jobs/',{
errors,
applicant,
email,
contact,
address,
position
});
如您所见,第二个参数不包含 Job
键。