Search code examples
javascriptnode.jsjsonexpresspug

PugJS not interpolating numbers from json


I'm trying to render a list of interfaces I get from my router API. It is formatted as JSON. Pug is only rendering the strings and not the numbers.

Output (it renders properly in html):

em1 192.168.0.0/24
Address: 192.168.0.1    
Received Packets: NaN    
Sent Packets: NaN

The JSON:

(em1) / 192.168.0.1":{"name":"em1","flags":"0x8943","network":"192.168.0.0/24","address":"192.168.0.1","received-packets":2524179,"received-bytes":247344321,"sent-packets":2453592,"sent-bytes":1695092680},"

The Pug code:

div.container
        each interface in interfaces
            div.interface
                h2.interface-name
                    | #{interface.name} #{interface.network}
                p Address: #{interface.address}
                p Received Packets: #{interface.received-packets}
                p Sent Packets: #{interface.sent-packets}

For the life of me I can't find anything that tells me what to do with native numbers. I'm using nodejs and express with pug.


Solution

  • Ok answering my own question. It appears dashes in identifier names don't work. In order to access that item, throw brackets and single quotes around it. So the following works:

      div.interface
                    h2.interface-name
                        | #{interface.name} #{interface.network}
                    p Address: #{interface.address}
                    p Received Packets: 
                        |#{interface['received-packets']}
                    p Received Bytes: #{interface['received-bytes']}
                    p Sent Packets: #{interface['sent-packets']}
    

    Got this from: Unable to access JSON property with "-" dash