问题
I want to show values of post object in my profilearea.ejs file.But when it renders to profilearea.ejs then it gives an error that "post is not defined".
Here is the code in node.js
PersonalInfo.findOne({username:req.body.name}, function(err,post){
if(err || !post)
{
console.log("find is not done");
}
else
res.render('profilearea.ejs', {post:post});
}
})
This is the code in profilearea.ejs file
<section id="notification" data-role="page" >
<header data-role="header" data-theme="b"><h2>INFORMATION</h2></header>
<div data-role="content">
<p> <%= post%> </p>
</div>
</section>
回答1:
I think part of the issue may be in not stringifying the object prior to rendering.
Try:
else {
var jpost = JSON.stringify(post);
res.render('profilearea.ejs', {post:jpost});
}
来源:https://stackoverflow.com/questions/12908967/when-i-pass-res-render-object-then-in-ejs-file-it-is-undefined