ErraticFox ErraticFox - 2 months ago 13
Node.js Question

Making list items out of object

I'm making a project Node.JS project with Socket.io. I'm trying to make HTML list items from a object. I'm trying to prerender players list to this pug file I'll be rendering. Though, I can get the players list and display it in the html, but all the usernames clump together as one list item.

Things I've tried

app.js

var players = {}

socket.on('connection', function(socket) {
players[socket.id] = {}
var playerList = ''
for (var i in players) {
playerList += i + "<br>"
}

var roomData = pug.renderFile('./views/room.pug', {
playerList: playerList
})
})


The reason
players[socket.id] = {}
is a object and not array is because there is other stuff that does go in the object, it's just not included in here.

room.pug

.row
.col.s12
#test
ul
li
|#{playerList}


As said before, the above method puts all the players in a single HTML list item. How can I seperate each player into their own list item? Thanks.

Answer

I would suggest:

socket.on('connection', function(socket) {
    players[socket.id] = {}
    var roomData = pug.renderFile('./views/room.pug', {
        playerList: Object.keys(players)
    })
    // etc ...
})

and in Pug:

.row
    .col.s12
        #test
            ul
                each playerName in playerList
                    li= playerName
Comments