【问题标题】:Wrong console output and template not rendering错误的控制台输出和模板未呈现
【发布时间】:2019-12-08 07:08:14
【问题描述】:

console.log 的输出中,将pre 添加到img 标记中。 其次,index ejs 模板也没有渲染。

app.set("view engine" , "ejs");
app.get('/', async ()=>
{
    let cont =  await data.find();
    let htmlContent;
    for(let x=0;x<cont.length;x++)
     htmlContent += `<img="${cont[0].image}"/><br> <p>${cont[0].content</p>`;
    console.log(htmlContent);
    app.render('index', {renderData:htmlContent});
} );

我想要的输出:

<img="sdlkfj"/><br><p>sdgdsfgfdshfd</p>

我得到的输出:

undefined<img="sdlkfj"/><br><p>sdgdsfgfdshfd</p>

【问题讨论】:

    标签: node.js express mongoose ejs


    【解决方案1】:

    htmlContent 未定义,然后向其添加一个字符串。尝试改用let htmlContent = "";

    【讨论】:

      【解决方案2】:

      您正在检查cont.length,但使用的是cont[0]。就是这样,cont[x]

      for(let x=0;x<cont.length;x++)
           htmlContent += `<img="${cont[x].image}"/><br> <p>${cont[x].content</p>`
      

      【讨论】:

        猜你喜欢
        • 2020-02-05
        • 1970-01-01
        • 2014-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多