Sebastian
Sebastian

Reputation: 96

Data from mysql in ejs but only get [object Object]

I only get [object Object] when I use EJS to render the data from MySQL. JSON.parse does not work as well with this string: "JSON.parse: unexpected character at line 1 column 2 of the JSON data"

app.get("/favorites", function(req, res) {
  var favorites = {};
  db.query("SELECT * FROM favs WHERE user_id = '1'", function (error, result, fields) {
    if (error) throw error;
    favorites = result;
    res.render("favorites", {data: {print: favorites, page: true}});
  });
});
console.log("<%= data.print %>");
<!-- [object Object] -->

Upvotes: 1

Views: 306

Answers (1)

Mureinik
Mureinik

Reputation: 311528

You have an object. You don't need to parse it, but to stringify it:

res.render("favorites", JSON.stringgify({data: {print: favorites, page: true}}));

Upvotes: 1

Related Questions