我试图在表单输入值字段中显示来自mongoDB的 - 检索 - 表单输入结果。但是,我收到错误'无法读取属性&未定义的长度'。请参阅下面的代码段 - ' retrieve.pug'正确显示检索到的数据,但无法重新提交,' profile.pug'如果我尝试在里面显示值,则会抛出错误。任何建议都非常感谢。非常感谢提前!!
以下是代码:
profile.pug - 显示带有检索值的表单输入 - >错误!
ul
each userinfo in userinfos
form(action='/', method='POST')
label(for='userinfo[firstName]') First name:
input(type='text', name="userinfo[lastName]", value=userinfo.firstName)
label(for='userinfo[lastName]') Last name:
input(type='text', name="userinfo[lastName]", value=userinfo.lastName)
label(for='userinfo[streetAddress]') Street address:
input(type='text', name="userinfo[streetAddress]", value=userinfo.streetAddress)
label(for='userinfo[state]') State:
input(type='text', name="userinfo[state]", value=userinfo.state)
label(for='userinfo[zip]') Zip:
input(type='text', name="userinfo[zip]", value=userinfo.zip)
label(for='userinfo[country]') Country:
input(type='text', name="userinfo[country]", value=userinfo.country)
label(for='userinfo[city]') city:
input(type='text', name="userinfo[city]", value=userinfo.country)
input(type='submit')
app.js - 填写表单输入并将其检索到' retrieve.pug' =>显示成功 - 但是,无法重新提交表单!
app.post('/retrieve', function(req, res){
Userinfo.findOneAndUpdate({_id: req.session.userId }, {$set:{ firstName:
req.body.firstName, lastName: req.body.lastName, streetAddress: req.body.streetAddress,
city: req.body.city, state: req.body.state, zip: req.body.zip, country: req.body.country,
postedBy: req.session.userId}}, {upsert: true, new: true}, function(err, Userinfo){
if(err){
if (err) res.json(err);
}
res.redirect('/retrieve');
console.log(Userinfo);
});
});
app.get('/retrieve', function(req, res){
Userinfo.find({ _id: req.session.userId }, function(err, docs){
if(err) res.json(err);
else res.render('retrieve', {userinfos: docs});
});
});