Trying out Sails with a new controller, but the view doesn't render correctly. My controller looks like this:
/**
* QuestionController
*
* @module :: Controller
* @description :: Contains logic for handling requests.
*/
module.exports = {
/* e.g.
sayHello: function (req, res) {
res.send('hello world!');
}
*/
index: function (req, res) {
return res.view({
questions: [{title: 'Freddy a presidente?', content: 'Últimamente Freddy se encuentra con más diplomacia.'}]
});
}
};
While my view looks like this:
<h1>Preguntas</h1>
<ul>
<% _.each(questions, function(question) { %>
<li><%= question.title %></li>
<% }) %>
</ul>
But I'm getting the following error:
ReferenceError: /home/alainus/dev/parlamenta/parlamenta/views/question/index.ejs:3
1| <h1>Preguntas</h1>
2| <ul>
>> 3| <% _.each(questions, function(question) { %>
4| <li><%= question.title %></li>
5| <% }) %>
6| </ul>
questions is not defined
at eval (eval at <anonymous> (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs/lib/ejs.js:236:14), <anonymous>:29:68)
at eval (eval at <anonymous> (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs/lib/ejs.js:236:14), <anonymous>:29:228)
at /home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs/lib/ejs.js:249:15
at Object.exports.render (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs/lib/ejs.js:287:13)
at Object.exports.renderFile (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs/lib/ejs.js:317:20)
at View.module.exports [as engine] (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs-locals/index.js:85:7)
at View.render (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/express/lib/view.js:76:8)
at Function.app.render (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/express/lib/application.js:506:10)
at ServerResponse.res.render (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/express/lib/response.js:756:7)
at renderView (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/lib/hooks/views/index.js:298:20)
at /home/alainus/dev/parlamenta/parlamenta/node_modules/sails/lib/hooks/views/index.js:280:20
at Object.oncomplete (fs.js:107:15)
Any ideas on how to make this work?
try putting quotes around questions
return res.view({
"questions": [{title: 'Freddy a presidente?', content: 'Últimamente Freddy se encuentra con más diplomacia.'}]
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With