Harp Server, Jade and JSON Loop, Error: Cannot read property "length" of undefined

Harp Server, Jade and JSON Loop, Error: Cannot read property "length" of undefined

这是 nav.jade 文件

each link in data
h5.home-link: a(href="#{ url }")

这是我的 data.json 文件

{
      "links": [
        { "url": "index.html", "pg-title": "Home" },
        { "url": "Wooden_Wall_Panelling.html", "pg-title": "Wooden Wall Panelling" },
        { "url": "Wooden_Partitioning.html", "pg-title": "Wooden Partitioning" },
        { "url": "Wardrobe_MDF_Shutters.html", "pg-title": "Wardrobe MDF Shutters" },
        { "url": "MDF_Ceiling_Patterns.html", "pg-title": "MDF Ceiling Patterns" },
        { "url": "Wooden_Grills.html", "pg-title": "Wooden Grills" }
     ]
}

错误(当 Jade 尝试编译文件时):

Cannot read property 'length' of undefined.

错误指向第一行,即数据中的每个 link。

我做错了什么?

我正在使用 Harp 服务器。

首先,确保它是一个 _data.json 文件,前面有一个下划线。然后,您的 nav.jade 文件应如下所示:

each link in public._data.links
  h5.home-link: a(href="#{ link.url }") #{ link["pg-title"] }

如果您在 _data.json 文件中将 pg-title 更改为 title,您还可以使语法更简洁:

each link in public._data.links
  h5.home-link: a(href="#{ link.url }") #{ link.title }

如果需要,您也可以删除字符串插值:

each link in public._data.links
  h5.home-link: a(href=link.url)= link.title

还有更多关于 metadata in Harp here 的信息。如果你正在调试并想看看你的所有元数据在 Harp 中是如何可用的,你可以暂时显示整个 public _data 对象:

pre: code= JSON.stringify(public._data, 0, 2)