简体   繁体   English

使用Node.js渲染pug中的数据-渲染页面上没有数据

[英]Using Node.js to render data in pug - no data on rendered page

I'm trying to use res.render() to pass data to a Pug template. 我正在尝试使用res.render()将数据传递到Pug模板。 When I get to this point in my code: 当我在代码中达到这一点时:

const _renderPage = function(req, res, responseBody) {  
console.log(responseBody);
res.render('page-template', {  
    pageData: responseBody,
    pageTitle: 'this is the about page!' }
    ); 
};

console.log(responseBody) shows: console.log(responseBody)显示:

[  { _id: '5bda4cfc11922d1dc5961922',
    name: 'about',
    body: 'about ksdjflskjflksdfks' }  ]

However, my Pug template which looks like the following, will not print the actual values of pageData.name or pageData.body even though it does print the pageTitle variable: 但是,我的Pug模板如下所示,即使它确实打印了pageTitle变量,也不会打印pageData.name或pageData.body的实际值:

h1= pageTitle
p Welcome to #{pageData.name}

.row
  .col-12
    p #{pageData.body}

Your root issue is that responseBody is an array, and you are trying to access it as an object. 您的根本问题是responseBody是一个数组,并且您试图将其作为对象进行访问。

Change the render function to feed the first element in the array to the template and it will work as expected: 更改render函数,将数组中的第一个元素提供给模板,它将按预期工作:

res.render('page-template', {  
  pageData: responseBody[0],
  pageTitle: 'this is the about page!' }
); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM