如何解决EJS语法错误:“参数列表后缺少”

时间:2019-04-21 08:49:38

标签: ejs

我正在尝试解决此错误,但是我无法弄清楚问题出在哪里。我只是在EJS中动态打印出JSON文件。

我的路由文件:

      ExcelRoute.get('/table', (req, res) => {
           var guest = 'karan'
            var myData = [{
      "Main Category": "Men",
      "Category": "Shoes",
      "Sub-Category": "Running"
    },
    {
      "Main Category": "Women",
      "Category": "Clothing",
      "Sub-Category": "Tees"
    },
    {
      "Main Category": "Kids",
      "Category": "Accessories",
      "Sub-Category": "Bags"
    },
    {
      "Main Category": "Sports",
      "Category": "Sport",
      "Sub-Category": "Training"
    },
    {
      "Main Category": "Collection",
      "Category": "",
      "Sub-Category": ""
    },
    {
      "Main Category": "Sale",
      "Category": "",
      "Sub-Category": ""
    },
    {
      "Main Category": "",
      "Category": "",
      "Sub-Category": ""
    },
    {
      "Main Category": "",
      "Category": "",
      "Sub-Category": ""
    },
    {
      "Main Category": "",
      "Category": "",
      "Sub-Category": ""
    },
    {
      "Main Category": "",
      "Category": "",
      "Sub-Category": ""
    },
    {
      "Main Category": "",
      "Category": "",
      "Sub-Category": ""
    }
  ]

        res.render('indexpage', {
        user: myData,
        guest: guest
        })
      })

我一直在使用EJS使用EJS打印对象数组。但这给了我一个错误:

  

在参数列表之后缺少)。

我的HTML文件代码

          <form method="GET" action="table">
            <button onclick="loadTable()">GenTable</button>
          </form>

我的EJS文件代码:

    <body>
        <h1>Welcome <%=guest%></h1>
        <ul>
            <% user.forEach((menu)=>{ %>
                <a href="#"><li><%=menu.Main Category%></li></a>
                <a href="#"><li><%=menu.Category%></li></a>
                <a href="#"><li><%=menu.Sub Category%></li></a>
            <% }) %>
        </ul>
    </body>

1 个答案:

答案 0 :(得分:1)

您应将每个属性括在方括号中。

<% user.forEach((menu)=>{ %>
     <a href="#"><li><%=menu['Main Category']%></li></a>
     <a href="#"><li><%=menu['Category']%></li></a>
     <a href="#"><li><%=menu['Sub Category']%></li></a>  
 <% }) %>