首页 > 解决方案 > 如何使用 nodejs 和 express 渲染带有哈巴狗的 Neo4j 集合

问题描述

我在 Ubuntu 20.04.2 之上使用 neo4j 4.3.2 社区版

我需要在 nodejs/express 应用程序中使用 pug 显示 neo4j 密码查询的结果。

使用 neo4j 示例,使用以下代码很容易获得结果的原始列表:

router.get('/', (req, res, next) => {
    const skip = int( parseInt(req.query.offset) || 0 )
    const limit = int( parseInt(req.query.limit) || 25 )

    const params = {
        skip, limit
    }

    return req.neo4j.read(`
        MATCH (regions:Region) 
        return regions 
        SKIP $skip LIMIT $limit
    `, params)
        .then(res => {
         const regions = res.records.map(row => {
                return new Region(
                    row.get('regions')
                )
            })
            return {
                regions: regions.map(n => n.toJson()),
            }
       })

//        .then(res.render('region/regions', { "regions": res }))
        .then(json => res.send(json))
        .catch(e => next(e))

})

结果很简单:

{"regions":[{"name":"Lazio","uuid":"ca1c1021-92f4-412b-aa3d-6c997cba248e"},{"name":"Umbria","uuid":"4fd434a4-1eb7-4177-9a0d-d1f5aa5efdaf"},
....
{"name":"Liguria","uuid":"7612bf03-a5aa-4bda-b823-9fe361c63667"}]}

现在,我想使用以下.pug模板显示相同的结果:

extends ../layout

block content
  h1 Listing regions 
  table
    thead
      tr
        th uuid
        th Name

    tbody
      each region in regions
        tr
          td= region.name
          td= region.uuid

所以,在我的路线文件中,我改变了行

        .then(json => res.send(json))

与行

        .then(res.render('region/regions', { "regions": res }))

这会导致以下错误:

TypeError: /node/kebranode/src/views/region/regions.pug:15
    13| 
    14|     tbody
  > 15|       each region in regions
    16|         tr
    17|           td= region.name
    18|           td= region.uuid

Cannot read property 'length' of undefined

知道如何摆脱它吗?

标签: node.jsexpressneo4jcypherpug

解决方案


推荐阅读