Home > OS >  Can EJS files receive objects?
Can EJS files receive objects?

Time:11-10

Briefly I was trying to pass an object to an ejs file using node but for some reason it turns it into a string

I have code on node that calls an ejs file

        res.render(__dirname   '/EJS/game.ejs', {
            game: game.game,
            username: game.username,
            message: null
         })

Where in this case, the game.game is an object, but when it arrives in the ejs file it becomes a string written:

'[object Object]'

Came here to ask if there is a way to make EJS read an object, and if so which one

CodePudding user response:

 res.render(__dirname   '/EJS/game.ejs', {
        game: JSON.stringify(game.game),
        username: game.username,
        message: null
     })

I think you should use JSON.stringify() on the object so as to get the actual value in it, if this does not return the actual value perhaps you are reading the object wrong

  • Related