When I pass res.render object then in .ejs file it is undefined

蓝咒 提交于 2019-12-08 10:02:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!