简体   繁体   English

如何将数组变量从javascript传递到pug文件?

[英]How to pass an array variable from javascript to a pug file?

I am currently learning web development. 我目前正在学习网络开发。 I am trying to dynamically fill a page on server side by using pug and javascript. 我正在尝试使用pug和javascript动态填充服务器端的页面。 My Pug code is below 我的帕格代码如下

#products-list.products 
    - var product_list = #{product_list}
    -if(product_list)
      each product in product_list
        +product_element(product['name'],product['price'],product['image'])

product_element is a mixin, his code is below product_element是一个mixin,其代码如下

mixin product_element(name,prod_price,image)
a(href='./produits/${product.id}' title='En savoir plus...')
h2= name
img(alt='product' src='./assets/img/'+image)
p.price= prod_price
  small Prix
  |

My javascript code is here 我的JavaScript代码在这里

router.get("/produits", (req, res) => {dataBase.getProducts(null,null).then(
(products)=>{
  if(products)
  {
    console.log('Get /produits sending..');
    res.render("products", {
                            titre: "OnlineShop - Produits", 
                            products_count: products.length+" produits",
                            products_list:JSON.stringify(products)
                            });
  }
}).catch(
(err)=>{
});});

I get a valid products list after my request , my problem is during the rendering of the page, it seems that I dont correctly link the data to the pug file. 我的请求后得到了一个有效的产品列表,我的问题是在页面渲染期间出现的,看来我没有正确地将数据链接到pug文件。 I have searched everywhere but I havent found an answer. 我到处搜索,但没有找到答案。

Currently I have this error : SyntaxError: Unexpected character '#' 当前,我有此错误:SyntaxError:意外的字符'#'

First, let's start with the template. 首先,让我们从模板开始。 You don't have to re-declare variables inside the template, they are already there for you. 您不必在模板内重新声明变量,它们已经在您那里了。 Also note that if a variable doesn't exist then the each loop won't execute and nothing will be output. 还要注意,如果变量不存在,则each循环将不会执行,也不会输出任何内容。

Try this instead: 尝试以下方法:

#products-list.products 
  each product in product_list
    +product_element(product['name'],product['price'],product['image'])

Then, there will also be issues inside the mixin. 然后,mixin内部也会出现问题。 It expects to see four variables input but the very first line calls product.id which is not one of the variables listed in the mixin definition. 它期望看到四个变量输入,但是第一行调用product.id ,它不是mixin定义中列出的变量之一。 It would probably be better just to pass in the entire product object instead of separating it out into separate variables: 最好传入整个产品对象,而不是将其分成单独的变量:

mixin product_element(product)
a(href= './produits/' + product.id title='En savoir plus...')
  h2= product.name
  img(alt='product' src='./assets/img/'+ product.image)
  p.price= product.price
    small Prix

Which would change your template to this: 这会将您的模板更改为此:

#products-list.products 
  each product in product_list
    +product_element(product)

Finally, you need to pass the entire products list from the route into the template. 最后,您需要将整个产品列表从路线传递到模板中。 Simply pass the the entire resultset from the database into the template like this: 只需将整个结果集从数据库传递到模板中,如下所示:

router.get("/produits", (req, res) => {dataBase.getProducts(null,null).then(
(products)=>{
  if(products)
  {
    console.log('Get /produits sending..');
    res.render("products", { "product_list": products });
  }
}).catch(
(err)=>{
});});

This passes the array of products through to the template in a variable named "product_list". 这会将产品数组传递到名为“ product_list”的变量中的模板。

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

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