[英]Render array of objects in Jade / Pug
I don't find a lot of stuff for Pug / Jade templating, so I try here. 我没有为Pug / Jade模板找到很多东西,所以我试试这里。 I've been reading the documentation for iterations and also this link here . 我一直在阅读迭代文档, 这里也是这个链接。
I'm working with Node.js, Express, and pug. 我正在使用Node.js,Express和pug。 So far I have some function server side that collect some data about users (I'm building a fake dating website as a school project) So my code server side looks like this : 到目前为止,我有一些功能服务器端收集一些关于用户的数据(我正在建立一个假的约会网站作为学校项目)所以我的代码服务器端看起来像这样:
router.post('/matchaSearch', function (req, res) {
matchaSearch(pool, session.uniqueID)
.then((results) => {
res.results = JSON.stringify(results)
console.log('result', results)
res.render('./userMatch', {res})
})
.catch((err) => {
console.error('error', err)
res.status(500).send("we don't have any suggestions for you so far")
})
})
I can log results
in iterm and all my data is here, but when it comes to client side it's kinda different. 我可以在iterm中记录results
,我的所有数据都在这里,但是当涉及到客户端时,它有点不同。
h1
|Here are your suggestions
script.
console.log(!{res.results}[0].username)
ul
for username in res.results
li= username
Here I can log the first username from res.results
in my browser's console, but in my page my li is rendering : 在这里,我可以在浏览器的控制台中记录res.results
中的第一个用户名,但在我的页面中,我的li正在渲染:
Here are your suggestions 这是你的建议
.[ [
.{ {
." “。
.i 。一世
.d .D
." “。
.: 。:
.3 0.3
.3 0.3
can you understand my problem here ? 你能在这里理解我的问题吗? it's showing every char from my array of objects. 它显示了我的对象数组中的每个字符。 I just wanted to show the username as a link so I can display a suggestion page with some suggested users. 我只想将用户名显示为链接,以便我可以显示一些建议用户的建议页面。
I was wondering if maybe I can use jquery to render so html into my li and then call it but I'm facing a wall here as well. 我想知道是否可以使用jquery将html渲染到我的li然后调用它,但我也在这里面对墙。 Any help is more than welcome ! 任何帮助都非常欢迎! Thanks. 谢谢。
{res}
does not make any sense to javascript. {res}
对javascript没有任何意义。 you should provide template an object include key/value pairs: 你应该提供一个对象包含键/值对的模板:
res.render('./userMatch', {results: results})
and then your loop in template becames: 然后你的模板循环成为:
ul
for item in results
li= item.username
h1
|Here are your suggestions
script.
console.log(!{res.results}[0].username)
ul
for username in res.results
li= username.username
i think username is object and you are printing that whole 我认为用户名是对象,你打印整个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.