I'm working on a random quote machine. Here's my NODE stuff:
var express = require("express");
var app = express();
app.set("view engine", "ejs");
app.get("/", function(req,res){
var data = "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1";
res.render("home", {data: data});
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("SERVER STARTED");
});
Here's the EJS stuff...
<h1>HELLO</h1>
<li><%= data.data %></li>
Everything is going fine, except here's the issue...
"data.data" in the EJS brackets, it's just nothing. Nothing there. And I've wracked my brain trying to figure out what's going on, but I can't. Bracket notation didn't work either. Any ideas what's going wrong?
Thanks!
When you pass stuff to the renderer, the object you pass in the second parameter becomes a 'global' in the ejs rendered page. So when you set something like below:
res.render("home", { myString: "I like strings" });
...you can access it by referring to the thing inside the passed object:
<li><%=myString%></li>
so your data.data is pointing to nothing.
I have the same issue and this is my solution. Add .toJSON() after data:
<li><%=data.toJSON().data%></li>