daniel gon
daniel gon

Reputation: 99

pass a json result of a mysql query as a param to my ejs in Node/express

I have a query to get all my users. I have been doing res.json(rows) to display to screen until now. Now I want to pass the object obtain from the query to a ejs file and display it. If I do like my code below, the object pass is a string and I can´t iterate or obtain the fields.

What is the best way to achive what I´m trying to do?

router.get("/users", (req, res) => {
    const connection = getConnection();
    const newLocal = "SELECT * FROM client";
    connection.query(newLocal,(err, rows, fields) => {
        if (err) {
            console.log("Error "+err);
            res.sendStatus(500);
            return;
        }
        // res.json(rows);
        res.render("users.ejs", {users: JSON.stringify(rows)});
    }); 
});

Upvotes: 0

Views: 128

Answers (1)

jemiloii
jemiloii

Reputation: 25719

It's because you're turning your Array rows into a String. Remove JSON.stringify

Upvotes: 1

Related Questions