渲染Jade/Pug中的对象数组 [英] Render array of objects in Jade / Pug

查看:136
本文介绍了渲染Jade/Pug中的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Pug/Jade模板,我找不到很多东西,所以我在这里尝试. 我一直在阅读有关迭代的文档,并且还阅读了此链接这里.

I don't find a lot of stuff for Pug / Jade templating, so I try here. I've been reading the documentation for iterations and also this link here.

我正在使用Node.js,Express和pug.到目前为止,我有一些功能服务器端,它收集有关用户的一些数据(我正在将假约会网站建设为学校项目) 所以我的代码服务器端看起来像这样:

I'm working with Node.js, Express, and 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")
       })
     })

我可以在iterm中记录results,我的所有数据都在这里,但是对于客户端来说有点不同.

I can log results in iterm and all my data is here, but when it comes to client side it's kinda different.

h1
 |Here are your suggestions
script.
 console.log(!{res.results}[0].username)
ul
  for username in res.results
    li= username

在这里,我可以在浏览器的控制台中登录res.results中的第一个用户名,但是在我的页面上,我的李正在渲染:

Here I can log the first username from res.results in my browser's console, but in my page my li is rendering :

这是您的建议

.[

.{

."

.i

.d

."

.:

.3

.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.

我想知道是否可以使用jquery将html渲染到li中,然后调用它,但是我在这里也面临着一堵墙. 任何帮助都超过了欢迎! 谢谢.

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. Any help is more than welcome ! Thanks.

推荐答案

{res}对javascript没有任何意义.您应该为模板提供一个包含键/值对的对象:

{res} does not make any sense to 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

这篇关于渲染Jade/Pug中的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆