我尝试使用node,express和mongo创建一个模板电子邮件的应用,但我遇到了错误:TypeError: Cannot read property 'subject' of undefined
以下是相关代码:
app.js
app.post('/request', function(req, res){
var request = new requestInfo({
subject: req.body.subject,
toName: req.body.toName,
toEmail: req.body.toEmail,
feeWaiver: req.body.feeWaiver,
request : req.body.request
});
request.save(function(err) {
res.redirect('/review')
});
});
app.get('/review', function(req,res){
res.render('review', {
user: req.user,
subject: req.request.subject,
toName: req.request.toName,
toEmail: req.request.toEmail,
feeWaiver: req.request.feeWaiver,
request : req.request.request,
firstName: req.user.firstName,
lastName: req.user.lastName,
myOrg: req.user.myOrg,
myPosition: req.user.myPosition,
myPhone: req.user.myPhone,
myAddress: req.user.myAddress,
myAddressTwo: req.user.myAddressTwo,
myTown: req.user.myTown,
myState: req.user.myState,
myZip: req.user.myZip
});
});
request.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var requestInfo = new mongoose.Schema({
subject : String,
toName : String,
feeWaiver : Boolean,
toEmail : String,
resolved : { type: Boolean, default: false},
date : { type: Date, default: Date.now },
request : String,
siteUser_id : String
});
requestInfo.virtual('requestId').get(function() {
return this._id;
});
module.exports = mongoose.model('Request', requestInfo);
request.jade
extends layout
block content
p To: #{toEmail} Subject: #{subject}
p My name is #{firstName} #{lastName} and I am a #{position} at #{myOrg}. I am requesting the following information under the Illinois Freedom of Information Act:
p #{request}
if feeWaiver
p I ask that you waive any and all fees associated with the gathering of this information as I am collecting and reporting on this information in the public interest.
p I also ask that you cite reasons for any redactions pursuant to Illinois FOIA law.
p My contact info is as follows:
p #{firstName} #{lastName}
p #{myAddress}
p #{myAddressTwo}
p #{myTown}, #{myState} #{myZip}
p Phone: #{myPhone}
<br>
p Much appreciated,
p #{firstName} #{lastName}
我认为我的错误可能与/请求没有将信息传递给/ review有关,但我觉得/ review仍然可以从mongodb中提取数据。此外,如果我在/ review中注释掉请求模型中的所有信息,它会正确显示所有用户模型数据。